Skip to content

Instantly share code, notes, and snippets.

@cimourdain
Created June 24, 2024 07:43
Show Gist options
  • Save cimourdain/34bc876fe566ccd2bb3cb316d3b5f2bf to your computer and use it in GitHub Desktop.
Save cimourdain/34bc876fe566ccd2bb3cb316d3b5f2bf to your computer and use it in GitHub Desktop.
Typescript decorator template
function decoratorTemplate<
decoratedClassType,
decoratedMethodResponseType,
decoratorResponseType,
>(
_target: object,
key: string | symbol,
descriptor: TypedPropertyDescriptor<
(...originFnArguments: object[]) => decoratedMethodResponseType
>
): TypedPropertyDescriptor<
(...originFnArguments: object[]) => decoratorResponseType
> | void {
const originalHandler = descriptor.value!;
descriptor.value = function (
this: decoratedClassType,
...originFnArguments: object[]
) {
// pre-apply actions
// call original function
const originalHandlerResponse = originalHandler.apply(this, [
...originFnArguments,
]);
// post-apply actions
return originalHandlerResponse;
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment