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
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:
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: