'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?



Solution 1:[1]

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[]
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Daniel X Moore