Open
Labels
Domain: DecoratorsThe issue relates to the decorator syntaxSuggestionAn idea for TypeScriptWaiting for TC39Unactionable until TC39 reaches some conclusion
Description
Not sure if this is by design or not, but the following gives a compile error of "Decorators are not valid here" with TypeScript 1.8:
let testClass = new class {
testMethod(@myDecorator date: Date): any {
return date;
}
}();Reacted by Warren, Paul, Boris Cherny, GoToLoop, Aleh Kashnikau, Morgan Touverey Quilling, stringparts, Braden Snell, Mahmoud Masri, Aluan Haddad and 80 more
Metadata
Metadata
Assignees
Labels
Domain: DecoratorsThe issue relates to the decorator syntaxSuggestionAn idea for TypeScriptWaiting for TC39Unactionable until TC39 reaches some conclusion
Activity
mhegazy commentedon Mar 2, 2016
they were never enabled for class expressions. so the behavior has not changed. but they should.
[-]Parameter decorators not allowed on methods of anonymous classes[/-][+]Decorators not allowed classes expressions[/+]dcworldwide commentedon Feb 23, 2017
This also applies when using the new 2.2 mixin design.
I.e.
Is this something that should work?
selsamman commentedon Mar 10, 2017
Not being able to use decorators with the mixin pattern for us means not being able to use the new mixin pattern at all. Is there a new milestone for this?
43081j commentedon Apr 27, 2017
any movement on this? we're still unable to use the new mixin pattern because of this.
it seems fixable by naming the class but should probably work on anonymous classes too...
selsamman commentedon Apr 27, 2017
If you follow the link above #14607, it seems there is a simple work around. The whole problem seems to be that you can't return directly a class with decorators but if you define the class with a local name and then return it everything is good. We have been using that pattern and taking advantage of mixins with decorators.
20 remaining items
canonic-epicure commentedon Jun 1, 2021
There is PR for this feature, which passes all CI checks.
DanielRosenwasser commentedon Jun 11, 2021
We discussed this again at #44555
The conclusion we reached in the design meeting is that the risk, work, and maintenance doesn't outweigh the benefits, especially given the workarounds available today. On the bright side, I've seen more progress in standardizing decorators over the last year, so it's hopefully not too far off.
brodo commentedon Jun 14, 2021
Daniel Rosenwasser (@DanielRosenwasser) is there a workaround that does not cause the TS4060 error? As Nickolay Platonov (@canonic-epicure) said, the ones posted here don't really address the issue.
canonic-epicure commentedon Jun 15, 2021
Julian Dax (@brodo) There's no workaround that works with declaration files generation, afaik. There is a "green" PR, that passes all tests and fixes this problem: #42198
Unfortunately, TS team decided that it should not be merged.
You can try using a fork of TypeScript with this PR merged like this:
RyanCavanaugh commentedon Jun 15, 2021
I can't offer a workaround since this is "out of spec" territory. Decorators are not supported on class expressions. We're going to wait until TC39 standardizes the decorator behavior.
ericmorand commentedon Jun 15, 2021
Ryan Cavanaugh (@RyanCavanaugh), out of curiosity, since there is no standard, what spec did you use as a reference for the support of decorators by TypeScript?
RyanCavanaugh commentedon Jun 15, 2021
The prior decorators proposal that was being worked on by TC39 around 2014, but was then abandoned in favor of a new decorators proposal which is now also at stage 2.
See also https://github.com/tc39/proposal-decorators#how-does-this-proposal-compare-to-other-versions-of-decorators
a-tarasyuk commentedon Mar 28, 2022
The decorators proposal has been moved to Stage 3.
MichelJonkman commentedon Dec 29, 2023
This still doesn't seem to work in Typescript 5.3, would be a very nice feature.
szymski commentedon Dec 29, 2023
Any particular obstacles which stop this from being implemented?
blipk commentedon Aug 19, 2024
If anyone else comes across this I managed to solve my issue by setting the following in tsconfig.json
"experimentalDecorators": trueAugustSaintFreytag commentedon Feb 23, 2025
The new TC39 proposal explicitly states:
I really hope this is going to be picked up soon. The change would greatly improve libraries implementing traits like
@ddd-ts/traitsthat rely on class expression syntax which currently can not be used together with property decorators.