-
Notifications
You must be signed in to change notification settings - Fork 12.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Check property declarations in JS #27023
Comments
Update: It seems more like a BUG?class Test {
/**
* @type {HTMLCanvasElement}
*/
_canvas; // IDE knows its type
} and class Test {
/**
* @type {HTMLCanvasElement}
*/
_canvas = document.createElement('canvas'); // IDE knows its type
} all take effect, but class Test {
constructor() {
this._canvas = document.createElement('canvas'); // Because this assignment
}
/**
* @type {HTMLCanvasElement}
*/
_canvas; // IDE no longer knows its type, as "any"
} doesn't take effects. |
Try this (might require typescript@next due to a recently fixed bug): class Test {
constructor() {
/**
* @type {HTMLCanvasElement}
*/
this._canvas = document.createElement('canvas');
}
} |
This works fine. However, how to turn class Test {
constructor() {
this._canvas = document.createElement('canvas'); // Because this assignment
}
/**
* @type {HTMLCanvasElement}
*/
_canvas; // IDE no longer knows its type, as "any"
} work? |
@andy-ms I think typescript itself can recognize the type but IDE can't. class Test {
constructor() {
this._canvas = document.createElement('canvas');
}
/**
* @type {HTMLCanvasElement}
*/
_canvas;
} The following statement's error can be recognized, underlined by a wave line: this._canvas = 0; // Error is occured once @ts-check is enabled:
// Cannot assign number 2 to type HTMLCanvasElement. But:
|
Update: It's fun:Working: Not Working: The difference is the order of member declaration. Source code: class Test1 {
constructor() {
this._canvas = document.createElement('canvas');
}
/**
* @type {HTMLCanvasElement}
*/
_canvas;
}
class Test2 {
/**
* @type {HTMLCanvasElement}
*/
_canvas;
constructor() {
this._canvas = document.createElement('canvas');
}
} |
@sandersn Above seems to be working now, but not: class Test1 {
x;
constructor() {
this.x = 0;
}
} which I think should succeed even in It looks like the class fields proposal allows fields without initializers. I think we should be able to infer a type in this case. It looks like currently in |
Recently I found it's helpful to add JSDOC style comment to hint my IDE the variable's type.
For example, If I add comment to a variable:
My IDE(Visual Studio Code Lastest) would correctly identify the type of
typedObject
.So that it give me a very nice intelligent prompt. Even when I enable the type check through
// @ts-check
, it will give the type errors.This feature is helpful to me and my project. It's written in Javascript(although we will try to rewrite it using Typescript in future but not for now).
We are using Babel with a plugin called
transform-class-properties
means that we can declare class member in ES6 class just like Typescript does:But when I add similar comment to
canvas
declaration, it can not be recognized by IDE. The IDE seems like only decides the type through its initializer. Could you support this feature please?The text was updated successfully, but these errors were encountered: