Skip to content

Instantly share code, notes, and snippets.

@SoEasy
Last active March 19, 2018 16:01
Show Gist options
  • Save SoEasy/c817ecd2e5302cf3a64ee08fc5ccb671 to your computer and use it in GitHub Desktop.
Save SoEasy/c817ecd2e5302cf3a64ee08fc5ccb671 to your computer and use it in GitHub Desktop.
TypeScript decorators types
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