Repository navigation
Dangerous Property Initializers in Classes #5987
Description
Activity
kitsonk commented
on Dec 30, 2015 ContributorAuthorMore actionsI suspect this got lost in the pile. Any thoughts, or is it just and edge case that isn't worth bothering with?
- addedSuggestionAn idea for TypeScriptAn idea for TypeScript
on Dec 30, 2015 DanielRosenwasser commented
on Dec 30, 2015 MemberMore actionsSorry 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.
- changed the title
[-]Dangerous Parameter Initializers in Classes[/-][+]Dangerous Property Initializers in Classes[/+]on Dec 30, 2015 kitsonk commented
on Dec 30, 2015 ContributorAuthorMore actionsClearly 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.
RyanCavanaugh commented
on Jan 1, 2016 MemberMore actions"Order of emit is order of declaration" is a must-have behavior for
ES6 compatES7+ compat as that would almost certainly have the same semantics.I don't see why we wouldn't error here, though. We can treat these as if they were
letdeclarations in terms of TDZ rules.- addedEffort: ModerateRequires experience with the TypeScript codebase, but feasible. Harder than "Effort: Casual".Requires experience with the TypeScript codebase, but feasible. Harder than "Effort: Casual".
on Jan 4, 2016 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; }());
Kitson Kelly (@kitsonk) Thoughts?
kitsonk commented
on Mar 22, 2018 ContributorAuthorMore actionsI 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).
Reacted by Seyi AdebajoJoshuaKGoldberg commented
on Jan 12, 2019 ContributorMore actionsThe 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,
x: any = this.x;is still allowed but shouldn't be.- added a commit that references this issue
on Jan 13, 2019 - added a commit that references this issue
on Apr 5, 2019 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: