[英]JSDoc: assign type to inline variable
Can I assign a type to a variable which gets a object
result from a third party library? 我可以为从第三方库获取
object
结果的变量分配类型吗?
const result = thirdPartyLib.doSomething();
Now I may have a ES6 class 现在我可能有一个ES6课程
class MyClass {
...
}
And I want to annotate my result
to be of type MyClass
. 我想将
result
注释为MyClass
类型。
Is this possible using JSDoc? 可以使用JSDoc吗?
The JSDoc @type {…}
tag can be applied to a local variable to declare its type. JSDoc
@type {…}
标记可以应用于局部变量以声明其类型。
/** @type {MyClass} */
const result = thirdPartyLib.doSomething();
However, instead of adding this declaration everywhere you call the function, you could use the @external
tag to add JSDoc types to thirdPartyLib.doSomething()
, allowing the local variable types to be inferred correctly. 但是,您可以使用
@external
标记将JSDoc类型添加到thirdPartyLib.doSomething()
,而不是在调用函数的所有位置添加此声明,从而可以正确推断局部变量类型。
/**
* @external thirdPartyLib
*/
/**
* @function external:thirdPartyLib.doSomething
* @returns {MyClass}
*/
const result = thirdPartyLib.doSomething();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.