-   Notifications  You must be signed in to change notification settings 
- Fork 1
Open
Description
You can either have types scoped to the level of the package, or you can export them, but you can't do both:
See
- https://github.com/coolaj86/test-case-tsc-exports
- https://stackoverflow.com/questions/73480632/how-can-i-export-package-scope-jsdoc-types
- JSDoc types.js cannot be used for package-level and export-level types microsoft/TypeScript#50436
⚠️  Workaround
 You can import your own exported types.
in package "foo"
// types.js /**  * @typedef {Object} Foo  * @prop {String} name  */// index.js /** @typedef {import('./types.js').Foo} Foo */ /** @type {Foo} */ let foo;in package "bar"
/** @type {import('foo/types.js').Foo} */ let foo;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:
{ "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  */Generate types.js
 ./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  */Metadata
Metadata
Assignees
Labels
No labels