I am currently working on creating ambient type definitions for a JavaScript utility package (similar to Lodash). I want users to be able to import modules in the following ways:
// For TypeScript or Babel
import myutils from 'myutils' // myutils = { a: [Function], b: [Function] }
import { a } from 'myutils' // a: Function
import a from 'myutils/a' // a: Function
// For JavaScript
const myutils = require('myutils') // myutils = { a: [Function], b: [Function] }
const { a } = require('myutils') // a: Function
const a = require('myutils/a') // a: Function
The package structure is as follows:
// a.js
module.exports = () => 'a'
// b.js
module.exports = () => 'b'
// index.js
module.exports = { a: require('./a'), b: require('./b') }
However, I am facing difficulties in writing proper TypeScript ambient definitions for these modules. This is what I have tried so far:
// a.d.ts
export default function a(): string
// b.d.ts
export default function b(): string
// index.d.ts
export { default as a } from './a'
export { default as b } from './b'
export default ...what?
I also attempted the following without success:
// index.d.ts
import a from './a'
import b from './b'
export { a, b }
// Member 'a' implicitly has an 'any' type, but a better type may be inferred from usage.ts(7045)
declare const _default: { a, b }
export default _default
Any insights would be greatly appreciated!