Created
July 29, 2017 19:41
-
-
Save attatrol/15d994e93e7f060e53b23a173678b221 to your computer and use it in GitHub Desktop.
typescript decorator for inherited classes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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