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
Request: Class Decorator Mutation #4881
Comments
|
Same would be useful for methods: class Foo {
@async
bar(x: number) {
return x || Promise.resolve(...);
}
}The async decorator is supposed to change the return type to |
|
@Gaelan, this is exactly what we are needing here! It would make mixins just natural to work with. class asPersistent {
id: number;
version: number;
sync(): Promise<DriverResponse> { ... }
...
}
function PersistThrough<T>(driver: { new(): Driver }): (t: T) => T & asPersistent {
return (target: T): T & asPersistent {
Persistent.call(target.prototype, driver);
return target;
}
}
@PersistThrough(MyDBDriver)
Article extends TextNode {
title: string;
}
var article = new Article();
article.title = 'blah';
article.sync() // Property 'sync' does not exist on type 'Article' |
|
+1 for this. Though I know this is hard to implement, and probably harder to reach an agreement on decorator mutation semantics. |
|
+1 |
|
If the primary benefit of this is introducing additional members to the type signature, you can already do that with interface merging: interface Foo { foo(): number }
class Foo {
bar() {
return this.foo();
}
}
Foo.prototype.foo = function() { return 10; }
new Foo().foo();If the decorator is an actual function that the compiler needs to invoke in order to imperatively mutate the class, this doesn't seem like an idiomatic thing to do in a type safe language, IMHO. |
|
@masaeedu Do you know any workaround to add a static member to the decorated class? |
|
@davojan Sure. Here you go: class A { }
namespace A {
export let foo = function() { console.log("foo"); }
}
A.foo(); |
|
It would also be useful to be able to introduce multiple properties to a class when decorating a method (for example, a helper that generates an associated setter for a getter, or something along those lines) |
|
The react-redux typings for |
|
I think the Currently it's Obviously the naming sucks and I have no idea if this sort of thing will work (I am just trying to convert a Babel app over to typescript and am hitting this). |
|
@joyt Could you provide a playground reconstruction of the problem? I don't use react-redux, but as I've mentioned before, I think any extensions you desire to the shape of a type can be declared using interface merging. |
|
@masaeedu here is a basic breakdown of the moving parts.. Basically the decorator provides a bunch of the props to the React component, so the generic type of the decorator is a subset of the decorated component, not a superset. Not sure if this is helpful, but tried to put together a non-runnable sample to show you the types in play. If you want a full working example I suggest pulling down https://github.com/jaysoo/todomvc-redux-react-typescript or another sample react/redux/typescript project. |
|
According to https://github.com/wycats/javascript-decorators#class-declaration, my understanding is that the proposed |
|
The spec says: is translate to: So if I understand it correctly, the following should be true: |
|
For your example:
I'm assuming you mean that declare function F<T>(target: T): typeof X;For that case, the assertions should be: let a: X = new Foo(); // valid
let b: Foo = new Foo(); // validThe let Foo = F(class Foo {}); |
|
@nevir Yep, you are right. Thanks for clarification. |
|
On a side note, it seems like turning off the check to invalidate mutated class types is relatively easy: But I am not knowledgable enough to make the compiler output the correct type definitions of the mutated class. I have the following test: tests/cases/conformance/decorators/class/decoratorOnClass10.ts It generates tests/baselines/local/decoratorOnClass10.types I was expecting |
|
For those interested in react-redux's |
|
@Joozty https://twitter.com/SeaRyanC/status/1402684762666373120 edit: I'm certain that the TypeScript team won't implement or consider anything in this regard until the decorator proposal is finished and supported by the browsers. |
|
My dream decorator: class Service {
@callbackify
public async handle(req: Request, res: Response){
// modern async code
}
}
const service = new Service();
// legacy callbacks-style framework
const app = express();
app.use( service.handle );
|
|
+1 |
|
so this is not solved yet? |
|
|
|
+1 |
1 similar comment
|
+1 |
|
I know this is an open issue, but I wanted to chime in given the revamped support for ES6 decorators in TypeScript 5.0. Decorators can mutate classes by returning an updated value, and this works at runtime, but augmented types are essentially discarded. Here's a TS Playground link for what would ideally be supported one day. |
|
Really need this feature. Please add it in TypeScript 6 |


If we can get this to type check properly, we would have perfect support for boilerplate-free mixins:
The text was updated successfully, but these errors were encountered: