Created
January 22, 2020 15:31
-
-
Save tspringborg/e5918f17cbe25dc6aa989cd010330d12 to your computer and use it in GitHub Desktop.
example methodDecorator typescript
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
// use like this | |
/* | |
@ExampleMethodDecorator({ | |
// whatever.... | |
foo: 2, | |
bar: 'foobar', | |
}) | |
*/ | |
export function ExampleMethodDecorator(configuration?: any): MethodDecorator { | |
return (target: any, propertyKey: string, descriptor: PropertyDescriptor) => { | |
const originalMethod = descriptor.value; | |
return { | |
get(this): any { | |
descriptor.value = (...args: any[]) => { | |
// args are the arguments supplied to the originalMethod | |
if (configuration) { | |
// configuration is the object supplied to the decorator | |
console.log(configuration) | |
} | |
const aNumber = Number(window.prompt("Type a number", "")); | |
if (aNumber) { | |
return originalMethod.apply(this, {...args, number: aNumber}) | |
} else { | |
return; | |
} | |
}; | |
// tslint:disable-next-line:no-non-null-assertion | |
return descriptor.value!.bind(this); | |
} | |
}; | |
}; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment