1

I would like to understand what are the different parameters of a Typescript decorator.

function myDecorator(target) {
  // do something with 'target' ...
}

In the example above, I know that target represents the function/class the decorator is attached to, but what are the other parameters of a decorator, their meaning an order, I also would like to get a link to an official documentation specifying this.

Thanks in advance.

3
  • TypeScript Documentation But you should also read: How much research effort is expected of Stack Overflow users? "Asking a question on Stack Overflow should be the last step in your process for finding an answer" I searched for "typescript decorator" with my search engine and this was the first result. Less than 30 seconds of research effort. Commented Feb 27, 2021 at 11:12
  • @ThomasSablik I checked the official documentation in your comment before posting this and I couldn't find the information I'm looking for there. Commented Feb 27, 2021 at 11:27
  • "I couldn't find the information I'm looking for there." You should check again. I found it there. Each decorator type has its own list. Commented Feb 27, 2021 at 12:04

1 Answer 1

3

A decorator expects one argument, the target it decorates and some more arguments depending on the type of the target, e.g.

Method Decorators

The expression for the method decorator will be called as a function at runtime, with the following three arguments:

  • Either the constructor function of the class for a static member, or the prototype of the class for an instance member.
  • The name of the member.
  • The Property Descriptor for the member.

You can find the complete lists on TypeScript Decorators for all decorator types:

  • Class Decorators

    • Only target class
  • Method Decorators

    • Either the constructor function of the class for a static member, or the prototype of the class for an instance member,
    • The name of the member,
    • The Property Descriptor for the member
  • Accessor Decorators

    • Either the constructor function of the class for a static member, or the prototype of the class for an instance member.
    • The name of the member.
    • The Property Descriptor for the member.
  • Property Decorators

    • Either the constructor function of the class for a static member, or the prototype of the class for an instance member.
    • The name of the member.
  • Parameter Decorators

    • Either the constructor function of the class for a static member, or the prototype of the class for an instance member.
    • The name of the member.
    • The ordinal index of the parameter in the function’s parameter list.

Additionally:

If we want to customize how a decorator is applied to a declaration, we can write a decorator factory. A Decorator Factory is simply a function that returns the expression that will be called by the decorator at runtime.

[TypeScript Decorators]

You can create decorator factories with parameters as you need. They're not limited or specified.

The examples from TypeScript documentation.

Decorator:

function sealed(target) {
  // do something with 'target' ...
}

applied with

@sealed x

Decorator factory:

function color(value: string) {
  // this is the decorator factory
  return function (target) {
    // this is the decorator
    // do something with 'target' and 'value'...
  };
}

applied as

@color('blue') x
Sign up to request clarification or add additional context in comments.

2 Comments

Why is this in the documentatoin : return function ( target, propertyKey: string, descriptor: PropertyDescriptor ) { console.log("f(): called"); }; (This is a decorator returned by a decorator factory and it has 3 parameters?
@Platus I changed my answer. The arguments depend on the type of the target and are listed in the link.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.