How to annotate an ES5 class with a JSDoc @template in VSCode to prevent checkJS errors?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
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.
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. 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.
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:
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
.
Changing the conditionList
declaration to 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
.
Therefore I have no idea on what to do in order to allow the template to work, while keeping the ES5 class declaration style.
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. In that case, hovering over conditionList
results in a popup saying var conditionList: Condition<string>
and there are no errors.
Is there any way to get the same behavior using the ES5 style classes? Am I missing something? Or might this be something unimplemented in VSCode's TypeScript checker?
javascript visual-studio-code annotations jsdoc checkjs
add a comment |
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.
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. 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.
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:
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
.
Changing the conditionList
declaration to 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
.
Therefore I have no idea on what to do in order to allow the template to work, while keeping the ES5 class declaration style.
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. In that case, hovering over conditionList
results in a popup saying var conditionList: Condition<string>
and there are no errors.
Is there any way to get the same behavior using the ES5 style classes? Am I missing something? Or might this be something unimplemented in VSCode's TypeScript checker?
javascript visual-studio-code annotations jsdoc checkjs
Looks like@template
is not officially supported. I guess here could be some implementation bugs in IDEs regarding this tag.
– Lesha Ogonkov
Nov 17 '18 at 7:14
I'm guessing it's a bug then. Incidentally, the template expressionT
is recognized in the prototype functions ofCondition
, even thoughCondition
itself does not use the template when instantiated. Also, prototype functions cannot have their own templates, which leads me to believe the@template
implementation is unfinished.
– José Cadete
Nov 17 '18 at 9:55
add a comment |
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.
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. 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.
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:
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
.
Changing the conditionList
declaration to 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
.
Therefore I have no idea on what to do in order to allow the template to work, while keeping the ES5 class declaration style.
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. In that case, hovering over conditionList
results in a popup saying var conditionList: Condition<string>
and there are no errors.
Is there any way to get the same behavior using the ES5 style classes? Am I missing something? Or might this be something unimplemented in VSCode's TypeScript checker?
javascript visual-studio-code annotations jsdoc checkjs
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.
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. 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.
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:
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
.
Changing the conditionList
declaration to 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
.
Therefore I have no idea on what to do in order to allow the template to work, while keeping the ES5 class declaration style.
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. In that case, hovering over conditionList
results in a popup saying var conditionList: Condition<string>
and there are no errors.
Is there any way to get the same behavior using the ES5 style classes? Am I missing something? Or might this be something unimplemented in VSCode's TypeScript checker?
javascript visual-studio-code annotations jsdoc checkjs
javascript visual-studio-code annotations jsdoc checkjs
edited Nov 16 '18 at 17:57
José Cadete
asked Nov 16 '18 at 17:40
José CadeteJosé Cadete
63
63
Looks like@template
is not officially supported. I guess here could be some implementation bugs in IDEs regarding this tag.
– Lesha Ogonkov
Nov 17 '18 at 7:14
I'm guessing it's a bug then. Incidentally, the template expressionT
is recognized in the prototype functions ofCondition
, even thoughCondition
itself does not use the template when instantiated. Also, prototype functions cannot have their own templates, which leads me to believe the@template
implementation is unfinished.
– José Cadete
Nov 17 '18 at 9:55
add a comment |
Looks like@template
is not officially supported. I guess here could be some implementation bugs in IDEs regarding this tag.
– Lesha Ogonkov
Nov 17 '18 at 7:14
I'm guessing it's a bug then. Incidentally, the template expressionT
is recognized in the prototype functions ofCondition
, even thoughCondition
itself does not use the template when instantiated. Also, prototype functions cannot have their own templates, which leads me to believe the@template
implementation is unfinished.
– José Cadete
Nov 17 '18 at 9:55
Looks like
@template
is not officially supported. I guess here could be some implementation bugs in IDEs regarding this tag.– Lesha Ogonkov
Nov 17 '18 at 7:14
Looks like
@template
is not officially supported. I guess here could be some implementation bugs in IDEs regarding this tag.– Lesha Ogonkov
Nov 17 '18 at 7:14
I'm guessing it's a bug then. Incidentally, the template expression
T
is recognized in the prototype functions of Condition
, even though Condition
itself does not use the template when instantiated. Also, prototype functions cannot have their own templates, which leads me to believe the @template
implementation is unfinished.– José Cadete
Nov 17 '18 at 9:55
I'm guessing it's a bug then. Incidentally, the template expression
T
is recognized in the prototype functions of Condition
, even though Condition
itself does not use the template when instantiated. Also, prototype functions cannot have their own templates, which leads me to believe the @template
implementation is unfinished.– José Cadete
Nov 17 '18 at 9:55
add a comment |
1 Answer
1
active
oldest
votes
It seems to be a bug, and has an open issue in Github. It seems it will not be fixed until TypeScript 3.3 is out unfortunately:
https://github.com/Microsoft/TypeScript/issues/26883
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53342856%2fhow-to-annotate-an-es5-class-with-a-jsdoc-template-in-vscode-to-prevent-checkjs%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
It seems to be a bug, and has an open issue in Github. It seems it will not be fixed until TypeScript 3.3 is out unfortunately:
https://github.com/Microsoft/TypeScript/issues/26883
add a comment |
It seems to be a bug, and has an open issue in Github. It seems it will not be fixed until TypeScript 3.3 is out unfortunately:
https://github.com/Microsoft/TypeScript/issues/26883
add a comment |
It seems to be a bug, and has an open issue in Github. It seems it will not be fixed until TypeScript 3.3 is out unfortunately:
https://github.com/Microsoft/TypeScript/issues/26883
It seems to be a bug, and has an open issue in Github. It seems it will not be fixed until TypeScript 3.3 is out unfortunately:
https://github.com/Microsoft/TypeScript/issues/26883
answered Nov 17 '18 at 18:00
José CadeteJosé Cadete
63
63
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53342856%2fhow-to-annotate-an-es5-class-with-a-jsdoc-template-in-vscode-to-prevent-checkjs%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Looks like
@template
is not officially supported. I guess here could be some implementation bugs in IDEs regarding this tag.– Lesha Ogonkov
Nov 17 '18 at 7:14
I'm guessing it's a bug then. Incidentally, the template expression
T
is recognized in the prototype functions ofCondition
, even thoughCondition
itself does not use the template when instantiated. Also, prototype functions cannot have their own templates, which leads me to believe the@template
implementation is unfinished.– José Cadete
Nov 17 '18 at 9:55