Options
All
  • Public
  • Public/Protected
  • All
Menu

reflect-helper

Index

Functions

decorate

  • decorate(target: Constructor<any> | Function, decorators: ClassDecorator | ClassDecorator[], properties?: PropertyDecorators): void
  • decorate(target: Constructor<any> | Function, properties: object): void
  • A helper function to apply decorators in ES5.

    Parameters

    • target: Constructor<any> | Function

      The constructor function or function that the decorators will be applied to.

    • decorators: ClassDecorator | ClassDecorator[]

      A decorator or array of decorators to apply to the type or function.

    • Optional properties: PropertyDecorators

      A map of decorators to be applied to the properties of a type. The key is the name of the property and the value is a decorator or an array of decorators.

      Basic example

      Suppose you have a decorator, Entity, that marks a class as a persistent entity. We can apply the decorator to our type as follows:

      function Person(name) {
          this.name = name;
      }
      
      decorate(Person, Entity());
      

      An array of decorators can be applied to a type:

      decorate(Person, [Entity(), Collection("people")]);
      

      Example with property decorators

      Suppose we have a decorator factory, Type, that gives the type of a field for a persistent entity. We can apply the decorator to a property of our type as follows:

      function Person(name) {
          this.name = name;
      }
      
      decorate(Person, Entity(), {
          name: Type(String)
      });
      

      An array of decorators can be applied for a property:

      decorate(Person, Entity(), {
          name: [Type(String), Field("n")]
      });
      

    Returns void

  • Parameters

    • target: Constructor<any> | Function
    • properties: object
      • [x: string]: function | Array<function>

    Returns void

getType

  • Gets type metadata for the specified concrete type.

    Parameters

    Returns Type

makeDecorator

  • makeDecorator(annotationCtr: Constructor<any>): DecoratorFactory
  • Makes a decorator factory from an annotation. The resulting decorator factory can be applied to a class or property.

    Parameters

    • annotationCtr: Constructor<any>

      The annotation constructor.

      Example

      An annotation is just a class. So, supposed with have an annotation that marks a class as a persistent entity as follows:

       class EntityAnnotation {
      
           constructor(public name?: string) {
           }
       }
      

      We can turn that annotation into a class decorator. When applied, an instance of the annotation is created and added to the 'annotations' metadata for the class. The parameters for the decorator will be the same as the parameters for the class constructor. However, we may want to provide a type annotation for the decorator factory because the parameters cannot be inferred.

       var Entity: (name?: string) => ClassDecorator = makeDecorator(EntityAnnotation);
      

      The Entity decorator can be applied as follows:

       @Entity()
       class Person {
      
       }
      

    Returns DecoratorFactory

Generated using TypeDoc