Created
June 24, 2024 07:43
-
-
Save cimourdain/34bc876fe566ccd2bb3cb316d3b5f2bf to your computer and use it in GitHub Desktop.
Typescript decorator template
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
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