-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
useDefineForClassFields defaults to true when target is es2022, breaks decorators. #48814
Comments
In the tsconfig reference: And in the release notes: Open to adding something to the Decorators docs. We discussed other options in a design meeting and the only one we were sort of ok with was issuing a config diagnostic for |
I guess I missed those and was looking in the decorator docs, don't know why I didn't see the 4.3 notes mention. Why can't decorated fields opt-out of define semantics? We're seeing more and more users that are broken, and they get no errors at all. They're just assuming our library broke when it didn't change. |
Unfortunately the design notes only capture “That sounds horrifying to me” in answer to that question. From my memory, I think the sentiment was generally that experimental decorators are experimental, so we’re not going to break standards compliance elsewhere to make them keep working, and that users should simply not try to use them in combination with useDefineForClassFields, making the config diagnostic the only sensible mitigation. |
class C {
accessor foo = 'abc';
}
// becomes
class C {
#foo_backing_store = 'abc';
get foo() { return this.#foo_backing_store; }
set foo(v) { this.#foo_backing_store = v; }
} It will be a bit before TS support for Stage 3 Decorators is ready, but I have been considering introducing the |
The other approach would be to recommend your users use class C {
@observe
declare foo: string; // field not declared on class, but decorator will still run
constructor() {
this.foo = "abc";
}
}
|
I know that we can tell users to use |
A property descriptor isn't provided to a field decorator because there's no descriptor to modify. This decorator breaks because it uses |
This issue has been marked 'Working as Intended' and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
This is really unfortunate, IMO. The switching of this setting to default true in some cases is actively breaking our users. |
I tried to get some movement on this in another design meeting. I asked @rbuckton to update this issue with the summary of what we talked about; it looks like the design meeting notes aren’t up yet. |
Bug Report
useDefineForClassFields
defaults totrue
when target is set to es2022 or esnext. This is breaking lots of projects that use decorators.This was reported in #45584, but that issue was closed apparently because the title didn't include which versions of TypeScript set
useDefineForClassFields
and because the TypeScript playground doesn't exhibit this same behavior. I've included a reproduction fortsc
here.This behavior is not documented anywhere in the TypeScript docs that I can find.
🔎 Search Terms
useDefineForClassFields decorators
🕗 Version & Regression Information
Since 4.2.4? according to #45584
Present in 4.6.3 and 4.7.0-dev.20220422
⏯ Playground Link
This playground link does not demonstrate the problem, as it doesn't default
useDefineForClassFields
liketsc
does. But if you setuseDefineForClassFields
totrue
you will see that that decorators could not work because the field is not an instance property that shadows the decorator defined prototype accessors.Playground link with relevant code
This git repo reproduces the problem locally: https://github.com/justinfagnani/ts-decorators-bug
💻 Code
This does not work with target
es2022
:🙁 Actual behavior
useDefineForClassFields
defaults totrue
🙂 Expected behavior
Option 1:
useDefineForClassFields
defaults tofalse
like for other permutations ofmodule
andtarget
until the new behavior is documented and users are notified with a prominent breaking change notice on the decorators documentation and the release notes.Option 2: Use constructor initialization for decorated properties even if
useDefineForClassFields
istrue
. This will keep projects from breaking regardless of the config.Option 3: Disallow both
useDefineForClassFields: true
andexperimentalDecorators
in the same config.This behavior is breaking a large number of our users. Sometimes they are in control of their own tsconfig and can set
useDefineForClassFields
tofalse
, but sometimes the config is generated for them via some other tool and they don't know how to set the value. In this case their project is just broken and they blame our library, not the update to TypeScript that included the undocumented breaking change.The text was updated successfully, but these errors were encountered: