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
Decorators stop working with typescript > 4.2.4 #1985
Comments
@CaptainCodeman the repro link is to the base lit-html template right now. |
Usually when the decorators break it's because |
related #1914 |
The bundle config was as simple as possible: tsconfig.json:
rollup.config.js:
Adding |
Something must have been changed in the Rollup plugin then, since this option is still |
We have the same issue, and adding |
Searching for |
Yeah I noticed that too. It has to be set to |
@justinfagnani I think this is related to the janky version of Typescript Microsoft bake in to Visual Studio - we had the exact issue (all property decorators failing), it worked fine in production builds and VS Code, but anyone using Visual Studio 2019 and Typescript 4.2.4 got this error. @abdonrd's suggestion to add I've raised microsoft/TypeScript#45584 as it appears to be a bug in Typescript, not Lit. |
After much discussion on Typescript's issue forums it appears this change was very much by design, just undocumented. I've raise microsoft/TypeScript#45653 to address that, but the discussion (microsoft/TypeScript#45718) is very much about making this error report better, not fixing it. With that in mind should we change the Lit documentation? If so there are two options:
@property() declare count: number; // = 0 can't have field initialised any more I think the former is much more practical for most users upgrading Lit projects to TS 4.3 (or 4.2.4 with VS2019) or later. I think the latter is potentially best practice, as long term we want to emit JS fields for ES2022+ targets whenever we're not wrapping that with the decorator property get/set boilerplate. However, the declared initialiser are no longer in the emitted constructor, so these will all start as |
After a little testing with this I think there might be a good case for In older TSC or with let MyCounterElement = class MyCounterElement extends LitElement {
constructor() {
super(...arguments);
this.count = 0;
}
//... rest of impl
}
__decorate([property()], MyCounterElement.prototype, 'count'); Since 3.7 with let MyCounterElement = class MyCounterElement extends LitElement {
count = 0;
//... rest of impl
}
__decorate([property()], MyCounterElement.prototype, 'count'); // Does nothing as count already defined! This is a breaking change, but there's a quirk of the original implementation I don't think most users realise - This has two potential side effects:
If we switch to @customElement('my-counter')
class MyCounterElement extends LitElement {
@property() declare count: number; // declare ensures no JS emitted to stop __decorate, but also no initialiser
increment(e: Event) {
this.count = (this.count ?? 0) + 1; // fallback required to avoid set to NaN on first call
}
render() {
return html`
<button @click=${this.increment}>Increment</button>
<p>Count: ${this.count ?? 0}</p>
` // fallback to avoid displaying empty value
}
} But I think these might be desirable, as we've avoided the potential side effects, and been explicit to TS that the property will be set up by the decorator annotation and not part of the class definition. |
useDefineForClassFields is incompatible with experimentalDecorators, see lit/lit#1985
Note that vite 2.5.0 now sets |
TS can use class fields with decorators without turning off useDefineForClassFields at the cost of losing inline initialisation see lit#479 and lit/lit#1985 for discussion
Related to this, I just see this in the esbuild v0.14.0 changelog:
|
Closing based on @abdonrd's last comment and the info in this thread. Please open a new issue with a specific repro if any problem or confusion around this issue remains. Thanks! |
Description
Using a version of Typescript later than 4.2.4 appears to stop some decorators working,
query
andproperty
don't function, butcustomElement
doesSteps to Reproduce
bundle with rollup:
Live Reproduction Link
https://stackblitz.com/edit/lit-html
Expected Results
Actual Results
Browsers Affected
The text was updated successfully, but these errors were encountered: