Skip to content

Instantly share code, notes, and snippets.

@christopherbauer
Last active April 30, 2023 21:07
Show Gist options
  • Save christopherbauer/21bb4fc674ca52193b37f3235c0e2540 to your computer and use it in GitHub Desktop.
Save christopherbauer/21bb4fc674ca52193b37f3235c0e2540 to your computer and use it in GitHub Desktop.
/**
* An example decorator that takes no arguments
* @param originalMethod The target method to decorate
* @param _context The context type, can be one of ClassMemberDecoratorContext | ClassMethodDecoratorContext | ClassGetterDecoratorContext | ClassSetterDecoratorContext | ClassFieldDecoratorContext | ClassAccessorDecoratorContext. Try the Command+F12 shortcut to look at the documentation!
* @returns A function that runs the original method but does some other fun stuff!
*/
const MyDecorator = (
originalMethod: Function,
_context: ClassMethodDecoratorContext
) => {
function ReplacementMethod(this: any, ...args: any[]) {
const result = originalMethod.call(this, ...args);
return result;
}
return ReplacementMethod;
};
class Typescripter {
@MyDecorator
writeTypeyCode() {
return true;
}
@MyDecorator
writeTypeyCodeWithArgs(amount: number) {
return amount;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment