简体   繁体   English

在 JSDoc @typedef 中使用导入的类型

[英]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?如何将@typedef与导入的别名一起使用?

This can happen if you are only exporting interface or type from your .d.ts file and not function or const .如果您仅从.d.ts文件中导出interfacetype ,而不是functionconst ,则可能会发生这种情况。

This will work:这将起作用:

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

but only if in util.d.ts you have但只有在util.d.ts你有

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

instead of代替

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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