Options
All
  • Public
  • Public/Protected
  • All
Menu

Class Annotation

Represents a metadata annotation which can be applied to classes, constructor parameters, methods, properties, or method parameters via decorators.

Custom annotations are defined as subclasses of this class. By convention, all custom annotation classes should have a name which ends in "Annotation" such as "NameAnnotation".

To create a new annotation create a subclass of Annotation with a constructor that takes the parameters you are interested in storing, and save the appropriate information onto fields of the new instance. For your convenience, Annotation provides a default constructor which takes a map object and applies its properties onto the current instance, but you may replace it with a constructor that takes any arguments you wish.

You may wish to add type safety to the default constructor parameter. To do so, override the constructor and define it:

class XYZ extends Annotation {
constructor(
options : MyOptions
) {
super(options);
}
}

Annotations are applied by using decorators. When you define a custom annotation, you must also define a custom decorator:

const Name =
NameAnnotation.decorator();

You can then use that decorator:

@Name()
class ABC {
// ...
}

Hierarchy

Implements

Index

Constructors

constructor

Properties

Readonly $metadataName

$metadataName: string

Readonly ngMetadataName

ngMetadataName: string = undefined

Methods

applyToClass

  • applyToClass(target: any): this
  • Apply this annotation to a given target.

    Parameters

    • target: any

    Returns this

applyToConstructorParameter

  • applyToConstructorParameter(target: any, index: number): this
  • Apply this annotation instance to the given constructor parameter.

    Parameters

    • target: any
    • index: number

    Returns this

applyToMethod

  • applyToMethod(target: any, name: string): this
  • Apply this annotation instance to the given method.

    Parameters

    • target: any
    • name: string

    Returns this

applyToParameter

  • applyToParameter(target: any, name: string, index: number): this
  • Apply this annotation instance to the given method parameter.

    Parameters

    • target: any
    • name: string
    • index: number

    Returns this

applyToProperty

  • applyToProperty(target: any, name: string): this
  • Apply this annotation instance to the given property.

    Parameters

    • target: any
    • name: string

    Returns this

clone

  • clone(): this
  • Clone this annotation instance into a new one. This is not a deep copy.

    Returns this

toString

  • toString(): string

Static decorator

  • Construct a decorator suitable for attaching annotations of the called type onto classes, constructor parameters, methods, properties, and parameters. Must be called while referencing the subclass of Annotation you wish to construct the decorator for. E.g., for FooAnnotation, call FooAnnotation.decorator().

    Type parameters

    Parameters

    • this: AnnotationConstructor<T, TS>

      The Annotation subclass for which the decorator is being constructed

    • Optional options: AnnotationDecoratorOptions<T, TS>

      Allows for specifying options which will modify the behavior of the decorator. See the DecoratorOptions documentation for more information.

    Returns AnnotationDecorator<TS>

Static filter

Static getAllForClass

  • Get all instances of this annotation class attached to the given class. If called on a subclass of Annotation, it returns only annotations that match that subclass.

    Type parameters

    Parameters

    Returns T[]

Static getAllForConstructorParameters

  • Get all instances of this annotation class attached to the parameters of the constructor for the given class. If called on a subclass of Annotation, it returns only annotations that match that subclass.

    Type parameters

    Parameters

    • this: AnnotationConstructor<T, TS>
    • type: any

      The class where constructor parameter annotations should be checked for

    Returns T[][]

Static getAllForMethod

  • Get all instances of this annotation class attached to the given method. If called on a subclass of Annotation, it returns only annotations that match that subclass.

    Type parameters

    Parameters

    • this: AnnotationConstructor<T, TS>
    • type: any

      The class where the method is defined

    • methodName: string

      The name of the method to check

    Returns T[]

Static getAllForParameters

  • Get all instances of this annotation class attached to the parameters of the given method. If called on a subclass of Annotation, it returns only annotations that match that subclass.

    Type parameters

    Parameters

    • this: AnnotationConstructor<T, TS>
    • type: any

      The class where the method is defined

    • methodName: string

      The name of the method where parameter annotations should be checked for

    Returns T[][]

Static getAllForProperty

  • Get all instances of this annotation class attached to the given property. If called on a subclass of Annotation, it returns only annotations that match that subclass.

    Type parameters

    Parameters

    • this: AnnotationConstructor<T, TS>
    • type: any

      The class where the property is defined

    • propertyName: string

      The name of the property to check

    Returns T[]

Static getForClass

  • Get a single instance of this annotation class attached to the given class. If called on a subclass of Annotation, it returns only annotations that match that subclass.

    Type parameters

    Parameters

    Returns T

Static getForMethod

  • Get one instance of this annotation class attached to the given method. If called on a subclass of Annotation, it returns only annotations that match that subclass.

    Type parameters

    Parameters

    • this: AnnotationConstructor<T, TS>
    • type: any

      The class where the method is defined

    • methodName: string

      The name of the method to check

    Returns T

Static getForProperty

  • Get one instance of this annotation class attached to the given property. If called on a subclass of Annotation, it returns only annotations that match that subclass.

    Type parameters

    Parameters

    • this: AnnotationConstructor<T, TS>
    • type: any

      The class where the property is defined

    • propertyName: string

      The name of the property to check

    Returns T

Static getMetadataName

  • getMetadataName(): string

Generated using TypeDoc