You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
classFoo{privatebbb=this.aaa*7;// ^^^// used BEFORE assignment constructor(privateaaa:number){}}
The sample above yields a compile error because the newly emitted JS code will be this
// JS compliant code when using `useDefineForClassFields: true`classFoo{aaa;bbb=this.aaa*7;// ^^^^^// 💥constructor(aaa){this.aaa=aaa;}}
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
The text was updated successfully, but these errors were encountered:
Is this microsoft/TypeScript#50971? TS may not emit compile errors for all cases that useDefineForClassFields changes so you can still end up with runtime errors too
According to microsoft/TypeScript#45995 (comment), it looks like useDefineForClassFields will be deprecated in TS6 and removed in 6.5 - is this something that is on the radar?
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.
The sample above yields a compile error because the newly emitted JS code will be this
The old code would have been (currently is)
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 itselfThe text was updated successfully, but these errors were encountered: