Open coolaj86 opened 2 years ago
You can either have types scoped to the level of the package, or you can export them, but you can't do both:
See
You can import your own exported types.
// types.js /** * @typedef {Object} Foo * @prop {String} name */
// index.js /** @typedef {import('./types.js').Foo} Foo */ /** @type {Foo} */ let foo;
/** @type {import('foo/types.js').Foo} */ let foo;
Make index.js be a re-export with all of the local types, then generate the local types.js:
index.js
package.json:
package.json
{ "main": "index.js", // our types + package re-export "files": [ "foo.js", "types.js" ] }
npm pkg set scripts.prepublish="./bin/re-export-types"
index.js:
module.exports = require('./'); /** * @typedef {Object} Bar * @prop {Number} age */ /** * @typedef {Object} Foo * @prop {String} name */
types.js
./bin/re-export-types:
./bin/re-export-types
#!/bin/sh my_typedefs="$( grep typedef ./index.js | cut -d ' ' -f5 )" rm -f ./types.js { echo '// auto-generated with ./bin/re-export-types' echo '// DO NOT EDIT' echo '' echo '/**' for my_type in $my_typedefs; do echo " * @typedef {import('./').${my_type}} ${my_type}" done echo ' */' } >> ./types.js
/** * @typedef {import('./').Foo} Foo * @typedef {import('./').Bar} Bar */
Fixed with npx jswt reexport
npx jswt reexport
You can either have types scoped to the level of the package, or you can export them, but you can't do both:
See
⚠️ Workaround
You can import your own exported types.
in package "foo"
in package "bar"
Perfect Solution: Re-export with types
Make
index.js
be a re-export with all of the local types, then generate the local types.js:package.json
:index.js
:Generate
types.js
./bin/re-export-types
: