[英]How to annotate an ES5 class with a JSDoc @template in VSCode to prevent checkJS errors?
I am attempting to annotate all my JavaScript functions and class properties with JSDoc to enable proper type checking using TypeScript and the checkJS
option. 我试图用JSDoc注释我的所有JavaScript函数和类属性,以使用TypeScript和
checkJS
选项启用正确的类型检查。
However, since I want to support older browsers (IE9), and I do not want to transpile the code using Babel, I need to use ES5-era JavaScript and cannot use "class" to define a class. 但是,由于我想支持较旧的浏览器(IE9),并且不想使用Babel来转译代码,因此需要使用ES5-era JavaScript,并且不能使用“类”来定义类。 Therefore, I need to resort to plain old function prototypes.
因此,我需要求助于简单的旧函数原型。
In the project I'm working on, I have the following class: 在我正在从事的项目中,我有以下课程:
/**
* A condition class, executes a set function on results that meet their required conditions.
* @class
* @template T
*/
function Condition()
{
/** @type {{condition?: boolean, result: T}[]} A list of conditions to execute. */
this.list = new Array();
}
/**
* Adds a condition
* @param {{condition: boolean, result: T}} condition The object with the condition and the result to be sent to the executing function.
*/
Condition.prototype.add = function(condition)
{
this.list.push(condition);
}
/**
* Executes the added conditions.
* @param {function(T):void} action The function to be executed when a condition is true.
*/
Condition.prototype.execute = function(action)
{
this.list.forEach(function(condition) { if(condition.condition) action(condition.result); });
}
In this class, the result
element is always of the same type within the same class instance, making this a perfect use case of a template, as can be seen in the code. 在此类中,
result
元素在同一类实例中始终具有相同的类型,这使得它成为模板的完美用例,如代码所示。
However, when I attempt to use the class using, for example, the following code: 但是,当我尝试使用例如以下代码来使用该类时:
var conditionList = /**@type {Condition<string>} */(new Condition());
conditionList.add({ condition: true, result: 'yes' });
conditionList.execute(function(value) { console.log(value); });
I get the following error in VSCode: 我在VSCode中收到以下错误:
Type 'string' is not assignable to type 'T'. The expected type comes from property 'result' which is declared here on type '{ condition: boolean; result: T; }'
When I hover the mouse on conditionList
, the popup says: var conditionList: typeof Condition
. 当我将鼠标悬停在
conditionList
,弹出窗口显示: var conditionList: typeof Condition
。
Changing the conditionList
declaration to var /**@type {typeof Condition<string>} */(new Condition());
将
conditionList
声明更改为var /**@type {typeof Condition<string>} */(new Condition());
also does not work, and throws an error saying that typeof Condition
cannot be converted to typeof Condition
. 也不起作用,并抛出一个错误,指出
typeof Condition
不能被转换为typeof Condition
。
Therefore I have no idea on what to do in order to allow the template to work, while keeping the ES5 class declaration style. 因此,在保持ES5类声明样式的同时,我不知道如何做才能允许模板工作。
However, if i convert the code to an ES6-style class ( class Condition
), and I keep the annotations just as they are, then the code works properly. 但是,如果我将代码转换为ES6样式的类(
class Condition
),并且按原样保留注释,则代码可以正常工作。 In that case, hovering over conditionList
results in a popup saying var conditionList: Condition<string>
and there are no errors. 在那种情况下,将鼠标悬停在
conditionList
上将导致显示一个conditionList
为var conditionList: Condition<string>
的弹出窗口,并且没有错误。
Is there any way to get the same behavior using the ES5 style classes? 有没有办法使用ES5样式类获得相同的行为? Am I missing something?
我想念什么吗? Or might this be something unimplemented in VSCode's TypeScript checker?
还是在VSCode的TypeScript检查器中未实现?
It seems to be a bug, and has an open issue in Github. 这似乎是一个错误,并且在Github中有一个未解决的问题。 It seems it will not be fixed until TypeScript 3.3 is out unfortunately:
不幸的是,直到TypeScript 3.3发布,它似乎都无法修复:
https://github.com/Microsoft/TypeScript/issues/26883 https://github.com/Microsoft/TypeScript/issues/26883
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.