Skip to content

Instantly share code, notes, and snippets.

@tspringborg
Created January 22, 2020 15:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tspringborg/e5918f17cbe25dc6aa989cd010330d12 to your computer and use it in GitHub Desktop.
Save tspringborg/e5918f17cbe25dc6aa989cd010330d12 to your computer and use it in GitHub Desktop.
example methodDecorator typescript
// 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