-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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 not allowed classes expressions #7342
Comments
they were never enabled for class expressions. so the behavior has not changed. but they should. |
This also applies when using the new 2.2 mixin design. I.e.
Is this something that should work? |
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? |
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... |
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. |
Here is an example how to use decorators on mixins properties: http://stackoverflow.com/questions/41318403/implement-pure-class-mixins-with-typescript/43162485#43162485 |
Just figured out this workaround as well. Here's a simple repro showing where it works and doesn't. function testDecorator(): PropertyDecorator {
return function internal() {
console.log("testing");
};
}
class Works {
@testDecorator()
method(): string {
return "works";
}
}
function makeClassWorks() {
class Generated {
@testDecorator()
method(): string {
return "works";
}
}
return Generated;
}
function makeClassDoesntWork() {
return class {
@testDecorator()
method(): string {
return "doesn't work";
}
};
}
|
I just ran into this. I see it is a future milestone. Any chance it can get scheduled? |
I just ran into this also. |
My prediction is that we'll tackle this once decorators reach stage 3. |
It still doesn't work. Is it ever going to be fixed? |
This issue is still active, but with one update, suggested workaround doesn't work anymore. If I do something like export const testF = () =>
class Class {
@Decorator()
public property: string;
}; It would complain about decorators with: export const testF = () => {
class Class {
@Decorator()
property: string;
}
return Class;
}; It would complain about using private property: Any suggestions or workarounds? |
For now, ugly but working, just call your decorators as functions : class Some {
@decorate()
prop: string;
} becomes const Some = class {
prop: string;
}
decorate()(Some.prototype, 'prop');
// (...)
return Some; |
@daweedm wouldn't extracting |
I just had the same issue. For documentation purposes, I'll add that this bug concerns the class fields syntax too. Example: class RootClass {
nestedClass = class {
@decorator
test() {}
}
} Will result in
|
Is there news on that issue? It's kinda easy to work around but it's still ugly: const myFunctionThatReturnsAClass = () => {
class ToReturn {
@MyDecorator()
run() {}
}
return ToReturn;
} |
@ericanderson Try to compile the workaround code with declarations emit enabled, you'll receive an error. Any chance this issue can be resolved? |
There is PR for this feature, which passes all CI checks. |
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. |
@DanielRosenwasser is there a workaround that does not cause the TS4060 error? As @canonic-epicure said, the ones posted here don't really address the issue. |
@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:
|
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. |
@RyanCavanaugh, out of curiosity, since there is no standard, what spec did you use as a reference for the support of decorators by TypeScript? |
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. |
The decorators proposal has been moved to Stage 3. |
This still doesn't seem to work in Typescript 5.3, would be a very nice feature. |
Any particular obstacles which stop this from being implemented? |
If anyone else comes across this I managed to solve my issue by setting the following in tsconfig.json |
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:
The text was updated successfully, but these errors were encountered: