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?
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.
Decorators can be implemented in a number of different ways. One useful use-case for decorators involves using them with methods defined in a class. Decorating methods in the classes we create can extend the functionality of the defined method.
In TypeScript, you can create decorators using the special syntax @expression , where expression is a function that will be called automatically during runtime with details about the target of the decorator. The target of a decorator depends on where you add them.
By definition, a decorator is a function that takes another function and extends the behavior of the latter function without explicitly modifying it.
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
, whereexpression
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With