Skip to content

Instantly share code, notes, and snippets.

@attatrol
Created July 29, 2017 19:41
Show Gist options
  • Save attatrol/15d994e93e7f060e53b23a173678b221 to your computer and use it in GitHub Desktop.
Save attatrol/15d994e93e7f060e53b23a173678b221 to your computer and use it in GitHub Desktop.
typescript decorator for inherited classes
// typeof any non-abstract child of Base
type ChildOfBase = typeof Base & (new (...args: any[])=> Base);
// decorator
const ClassDecorator = function(valueA: string): (value: ChildOfBase) => ChildOfBase {
return (value: ChildOfBase) => {
value.prototype.DoSmthWithClassName = () => {
return value.name + ".DoSmth_" + value.A;
}
value.A = value.name + valueA;
return value;
}
}
abstract class Base {
public static A: string = "Base_A";
public DoSmthWithClassName(): string {
return Base.name + ".DoSmth_" + Base.A;
}
private C: string;
public constructor(...args: any[]);
public constructor(c: string) {
this.C = c;
}
}
@ClassDecorator("NewA")
class Derived extends Base {
constructor(c: string) {
super(c);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment