Search code examples
typescriptdecorator

Typescript decorator for function, not method. Possible?


I'm trying to add a custom TypeScript decorator in a function which is not included in a class and it seems that the compiler is complaining no matter what I do.

Any thoughts? Is it possible?


Solution

  • add a custom TypeScript decorator in a function

    Not to a raw function. The main issue is dealing with hoisting of functions. Any attempt to wrap a function in another function breaks the hoist.

    Support Targets

    A Decorator is a special kind of declaration that can be attached to a class declaration, method, accessor, property, or parameter. Decorators use the form @expression, where expression must evaluate to a function that will be called at runtime with information about the decorated declaration.

    Docs: https://www.typescriptlang.org/docs/handbook/decorators.html#decorators