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
{{ message }}
Repository navigation
useDefineForClassFields breaks augmentation/overwrite of class members with decorators #35081
Regarding this bug, it would be nice if the typescript team could align the implementation for decorators and define semantics for fields to the current babel implementation (legacy mode) this would make a lot of things much easier especially for framework developers who ship decorator libraries (currently I'm detecting and testing TS legacy, Babel Legacy and the Babel Stage 2 implementation) a 4th decorator flavor makes it just harder to maintain (yes I know decorators are highly experimental ;))
one thing regarding inheritance, currently the useDefineForClassFields just overrides the property descriptor of super (defines a new one on the instance), it would be nicer if something like the babel helper could be used: this will just update the value but leave a possibly augmented (by a decorator or manual) intact (just the initalizer value is overridden)
In relation to replacing a class field/property/method - following code works on existing implementations of decorators with exception of TypeScript fields when using useDefineForClassFields. Unfortunately this is breaking without workaround. If fully aligning implementation with either babels "modern" or "legacy" decorators is too much work, perhaps consider implementing #29299 Option to disable decorator transform instead and let people further transform them themselves.
Ay updates here? We're increasingly seeing users complain that their components are broken because of this issue, and the symptom is very hard to track back to this unless you already know about it.
For some reason many tools that integrate the TypeScript compiler are enabling useDefineForClassFields and some of them don't give users control of the config, so there's no good workaround (except to delete own properties which puts objects into dictionary mode).
Reacted by Serhii Kulykov, Peter Burns, Lukáš Novotný, Mathis Zeiher, brandon-marginalunit, Kirill Anoshin, Kari Söderholm, Danny McGee and Hugo Campos
We're still seeing this in TypeScript 4.1.0-dev.20201020. It breaks the Aurelia framework, which is blocking us from using a library that requires useDefineForClassFields.
I just ran into this because this seems to mean that it's not possible to use MobX decorators and lit-element decorators together with TypeScript. MobX decorators seem to require "useDefineForClassFields": true but that breaks lit-element decorators (like @property and @query).
Reacted by Hugo Campos, Zomono, Stephen Haney and KSQ
This isn't a bug, but rather the expected behavior. It is unfortunate, but is a consequence of several interactions:
--useDefineForClassFields mimics the proposed behavior of ECMAScript fields.
The proposed behavior for ECMAScript fields is that they are defined in the constructor (i.e., using Object.defineProperty).
Defining a field on an instance means that a prototype walk will not occur, so the get/set you are defining on the prototype will never occur.
Defining a field does not trigger setters.
Even declaring a field like myProp: string; with --useDefineForClassFields will result in a new field definition on the instance that will shadow the prototype.
We introduced declare fields in TS specifically for this reason:
functionDecorator(){returnfunction(target,propKey,descriptor){constsymbol=Symbol();target.constructor.prototype[symbol]=descriptor ? typeofdescriptor.initializer!=='undefined' ? descriptor.initializer() : undefined : undefined;return{configurable: true,enumerable: true,set(value){this[symbol]=value;},get(){returnthis[symbol]+'decorated';}}}}classTestTSDefine{
@Decorator()declaremyProp: string;// does not result in redefinition// ^^^^^^^constructor(){this.myProp="test";// initializer must be moved to constructor};}console.log(newTestTSDefine().myProp);
Babel's decorators support permits two things TypeScript never has:
Decorating a "descriptor" for a field.
A decorator can intercept the initializer of a field descriptor.
These were part of an early draft for stage-2 decorators that has since been abandoned. Implementing this would be a major change for TypeScript decorators, a feature which is essentially "frozen" until a stable decorators proposal reaches Stage 3 in TC39.
Changing the behavior of decorators is out of scope, and changing the field definition semantics in --useDefineForClassFields to be incompatible with the ECMAScript fields proposal is also out of scope.
Until a stable decorators proposal reaches consensus, we don't intend to make any substantial changes to our decorators implementation. It may not be much, but we added declare fields as an alternative to further complicating the decorator landscape.
The problem here is that some toolchains set this compiler flag and it breaks existing decorator usage. Previously, decorators could assume that they could define accessors and they would work. I think it would be reasonable to automatically use set semantics for decorated fields to keep them working even in the presence of this flag.
Activity
rictic commentedon Jan 22, 2020
Simpler example:
http://www.typescriptlang.org/play/?useDefineForClassFields=true&experimentalDecorators=true#code/FAYw9gdgzgLgBAEQKbgE4EMZlQLjgBVTAAclUYBPZNTbOAXjgAoBKBgPjgG9g4+5USGAFdUEbr35SA5kNYSpi-oJFi4AcgAmKbJiSa4AN3QAbYUnUBuSUoC+NuPdvXQJ9FChwAKklheAysgAZgCWEEgK-AAC1LpYqA4AthSEJAxwAETERKTkFHBhITAhpiEAXmRGpuYZ1vagkFBgJkgAdCZg0kzhAO7evjABwWFIrK3JqcQs1kA
mzeiher commentedon Mar 29, 2020
Regarding this bug, it would be nice if the typescript team could align the implementation for decorators and define semantics for fields to the current babel implementation (legacy mode) this would make a lot of things much easier especially for framework developers who ship decorator libraries (currently I'm detecting and testing TS legacy, Babel Legacy and the Babel Stage 2 implementation) a 4th decorator flavor makes it just harder to maintain (yes I know decorators are highly experimental ;))
one thing regarding inheritance, currently the useDefineForClassFields just overrides the property descriptor of super (defines a new one on the instance), it would be nicer if something like the babel helper could be used: this will just update the value but leave a possibly augmented (by a decorator or manual) intact (just the initalizer value is overridden)
Kukkimonsuta commentedon Apr 2, 2020
In relation to replacing a class field/property/method - following code works on existing implementations of decorators with exception of TypeScript fields when using
useDefineForClassFields. Unfortunately this is breaking without workaround. If fully aligning implementation with either babels "modern" or "legacy" decorators is too much work, perhaps consider implementing #29299 Option to disable decorator transform instead and let people further transform them themselves.Code here:
Gist
TypeScript playground
Babel repl
1 remaining item
justinfagnani commentedon Oct 8, 2020
Ay updates here? We're increasingly seeing users complain that their components are broken because of this issue, and the symptom is very hard to track back to this unless you already know about it.
For some reason many tools that integrate the TypeScript compiler are enabling
useDefineForClassFieldsand some of them don't give users control of the config, so there's no good workaround (except to delete own properties which puts objects into dictionary mode).brandon-marginalunit commentedon Oct 20, 2020
We're still seeing this in TypeScript
4.1.0-dev.20201020. It breaks the Aurelia framework, which is blocking us from using a library that requiresuseDefineForClassFields.Haprog commentedon Nov 10, 2020
I just ran into this because this seems to mean that it's not possible to use MobX decorators and lit-element decorators together with TypeScript. MobX decorators seem to require
"useDefineForClassFields": truebut that breaks lit-element decorators (like@propertyand@query).rbuckton commentedon Dec 16, 2020
This isn't a bug, but rather the expected behavior. It is unfortunate, but is a consequence of several interactions:
--useDefineForClassFieldsmimics the proposed behavior of ECMAScript fields.Object.defineProperty).get/setyou are defining on theprototypewill never occur.myProp: string;with--useDefineForClassFieldswill result in a new field definition on the instance that will shadow the prototype.We introduced
declarefields in TS specifically for this reason:Playground Link
rbuckton commentedon Dec 16, 2020
Babel's decorators support permits two things TypeScript never has:
initializerof a field descriptor.These were part of an early draft for stage-2 decorators that has since been abandoned. Implementing this would be a major change for TypeScript decorators, a feature which is essentially "frozen" until a stable decorators proposal reaches Stage 3 in TC39.
Changing the behavior of decorators is out of scope, and changing the field definition semantics in
--useDefineForClassFieldsto be incompatible with the ECMAScript fields proposal is also out of scope.Until a stable decorators proposal reaches consensus, we don't intend to make any substantial changes to our decorators implementation. It may not be much, but we added
declarefields as an alternative to further complicating the decorator landscape.justinfagnani commentedon Dec 16, 2020
The problem here is that some toolchains set this compiler flag and it breaks existing decorator usage. Previously, decorators could assume that they could define accessors and they would work. I think it would be reasonable to automatically use set semantics for decorated fields to keep them working even in the presence of this flag.