简体   繁体   中英

Using an imported type in JSDoc @typedef

I'm trying to reduce duplication in specifying my imported types as follows but I'm getting an error

/**
@typedef {import("../types/util")} util
@typedef {util.mapBehaviors} mapBehaviors
... lots of other typedefs based on util
*/
'util' only refers to a type, but is being used as a namespace here.

It's weird because expanding the import explicitly works:

/**
@typedef {import("../types/util").mapBehaviors} mapBehaviors
... lots of other typedefs
*/

How can I use @typedef with an alias of an import?

This can happen if you are only exporting interface or type from your .d.ts file and not function or const .

This will work:

/**
@typedef {import("../types/types").util} util
@typedef {util["mapBehaviors"]} mapBehaviors
...
*/

but only if in util.d.ts you have

export function mapBehaviors(tags: string[] | Behavior[], table: Behaviors): Behavior[]
...

instead of

export interface mapBehaviors {
  (tags: string[] | Behavior[], table: Behaviors): Behavior[]
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM