Typescript decorator for function, not method. Possible?

17,374

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

Share:
17,374
Nick Tsitlakidis
Author by

Nick Tsitlakidis

I am a software engineer and spend most of my time working with agile and high level technologies. I am a great fan of object oriented design and its best practices (design patterns, unit testing, etc) and try to use them in the projects I'm involved. I'm usually working in web or mobile platforms.

Updated on June 26, 2022

Comments

  • Nick Tsitlakidis
    Nick Tsitlakidis about 2 years

    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?