Last active
March 19, 2018 16:01
-
-
Save SoEasy/c817ecd2e5302cf3a64ee08fc5ccb671 to your computer and use it in GitHub Desktop.
TypeScript decorators types
This file contains hidden or 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
type PropertyDecorator = (target: any, propertyKey: string | symbol) => void; | |
type MethodDecorator = (target: any, propertyKey: string | symbol, descriptor: TypedPropertyDescriptor) => | |
TypedPropertyDescriptor | void; | |
type ClassDecorator = (target: any) => any; | |
function ClassDecoratorImpl(...decoratorArgs: Array<any>): ClassDecorator { | |
console.log('injectable works fine'); | |
return function(target) { | |
// common code для переопределения конструктора | |
const originalConstructor = target; | |
function construct(constructor: any, args: Array<any>): any { | |
// tslint:disable-next-line | |
const c: any = function () { | |
// tslint:disable-next-line | |
return constructor.apply(this, args); | |
}; | |
c.prototype = constructor.prototype; | |
return new c(); | |
} | |
// tslint:disable-next-line | |
const newConstructor = function (...args) { | |
console.log('Injected!'); | |
const newInstance = construct(originalConstructor, args); | |
return newInstance; | |
}; | |
newConstructor.prototype = originalConstructor.prototype; | |
return newConstructor; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment