-
Notifications
You must be signed in to change notification settings - Fork 37.2k
Description
Intro
Some background info: https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-7.html#the-usedefineforclassfields-flag-and-the-declare-property-modifier and https://www.typescriptlang.org/tsconfig#useDefineForClassFields
The gist is that JavaScript now supports declaring class fields (before they were implicitly defined by assignment). In TypeScript we always declared class fields but the JS variant is slightly different. The sample below will not work anymore because TS constructor fields get now declared too.
class Foo {
private bbb = this.aaa * 7;
// ^^^
// used BEFORE assignment
constructor(private aaa:number){}
}The sample above yields a compile error because the newly emitted JS code will be this
// JS compliant code when using `useDefineForClassFields: true`
class Foo {
aaa;
bbb = this.aaa * 7;
// ^^^^^
// 💥
constructor(aaa) {
this.aaa = aaa;
}
}The old code would have been (currently is)
class Foo {
constructor(aaa) {
this.aaa = aaa;
this.bbb = this.aaa * 7;
}
}Why
There is some hope that explicitly declared class properties prevents shape mutations which leads to deopting
Adoption
We have ~280 compile errors when enabling useDefineForClassFields which can be fixed with ease. Move field assignment that depends on a ctor-defined-field into the ctor itself