You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Would it be possible for the type checker to know about changes made to a class via decorators?
Currently if you use a decorator to add a method to the prototype of a class constructor, you need to add a placeholder method that is overridden, or add a matching declaration to the interface with the same name. It's not terribly onerous, but it'd be good if we could leave it out.
Use Cases
This could be used to create typesafe mixins of specific interfaces, and possibly a generic mixin mechanism that would be easy to use.
Examples
function bar() {
return "This is bar";
}
type Constructor<T> = { new(...args: any[]): T };
interface Decorated {
bar():string;
}
// I want the type checker to pay attention to the return type of the decorator
function Decorate<T>(clazz:Constructor<T>): Constructor<T & Decorated> {
clazz.prototype.bar = bar;
return clazz as Constructor<T & Decorated>;
}
interface Foo extends Decorated{} // I want to leave this out
@Decorate
class Foo {
foo() {
return "This is Foo"
}
}
const aFoo = new Foo();
console.log('foo told me', aFoo.foo());
console.log('foo also told me', aFoo.bar()); // This currently won't compile without the extra interface decl
Checklist
My suggestion meets these guidelines:
This wouldn't be a breaking change in existing TypeScript / JavaScript code
This wouldn't change the runtime behavior of existing JavaScript code
This could be implemented without emitting different JS based on the types of the expressions
This isn't a runtime feature (e.g. new expression-level syntax)
The text was updated successfully, but these errors were encountered:
Search Terms
Suggestion
Would it be possible for the type checker to know about changes made to a class via decorators?
Currently if you use a decorator to add a method to the prototype of a class constructor, you need to add a placeholder method that is overridden, or add a matching declaration to the interface with the same name. It's not terribly onerous, but it'd be good if we could leave it out.
Use Cases
This could be used to create typesafe mixins of specific interfaces, and possibly a generic mixin mechanism that would be easy to use.
Examples
Checklist
My suggestion meets these guidelines:
The text was updated successfully, but these errors were encountered: