-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Dangerous Property Initializers in Classes #5987
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
Comments
I suspect this got lost in the pile. Any thoughts, or is it just and edge case that isn't worth bothering with? |
Sorry about that, this past month's been busy and naturally some people are out. I wonder if this should fall under the umbrella of #2234. |
Clearly it is related to #2234, but I would suspect a lot of users would be "surprised" that they have to think about the ordering of a property initializers in a class, although I just checked the ES Stage 1 proposal for class properties and initializers and it appears that this would break there too, so the emit is valid. In that case, stupid is as stupid does and it would logically fall in the realm of use before initialization. cc/@adidahiya |
"Order of emit is order of declaration" is a must-have behavior for I don't see why we wouldn't error here, though. We can treat these as if they were |
Is this a similar issue to the ffg being allowed in TS? class Test {
x:any = this.x;
} I can't find any documentation that says you can reference this in a class field / property but TS allows this to compile and emits js, but looking at it seems odd. emit: var Test = /** @class */ (function () {
function Test() {
this.x = this.x;
}
return Test;
}()); |
@kitsonk Thoughts? |
I don't know if it is similar. It is valid, though illogical. My opinion would be that it should be an error as well (sort of the same class as use before assigned). |
The original code now correctly gives a type checking error! ✨ class One {
two() {};
}
class Foo {
two = this.one.two();
~~~ // Property 'one' is used before its initialization.ts(2729)
one = new One();
} However, |
Fixes microsoft#5987. Usages of a class property in a preceding property already gave an error, but the following doesn't yet: ```ts class Test { x: number = this.x; } ``` As with other use-before-declare checking, IIFEs are not treated as invalid uses.
…29395) * Added error for class properties used within their own declaration Fixes #5987. Usages of a class property in a preceding property already gave an error, but the following doesn't yet: ```ts class Test { x: number = this.x; } ``` As with other use-before-declare checking, IIFEs are not treated as invalid uses. * Accepted 'witness' baselines; removed unnecessary !== * Addressed quick feedback items * Accepted odd new baseline * Fixed post-merge introduced lint errors * Updated baselines again
Hey folks, the Ember TS crowd just noted an issue with the PR that closed this; see my comment here. |
This is valid, but seem very dangerous:
The emit goes in order of declaration which will cause a runtime error:
The text was updated successfully, but these errors were encountered: