Normal Programming Mistake

    TypeScript icon, indicating that this package has built-in type declarations

    1.1.1 • Public • Published

    This module is a utility library for working with Angular2-style annotations and type information provided by the TypeScript --emitDecoratorMetadata compiler option. An annotation is a class that can be added as metadata on a type, property, method, or parameter. API documentation is available here.


    Suppose you have a class, EntityAnnotation, that can be used to flag a type as a persistent entity. The annotation can optionally specify the name of the entity. You can create this annotation in TypeScript as follows:

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

    To be able to apply the annotation to a class, we must first turn it into a decorator factory. We can do this using the makeDecorator function. When the decorator is applied to a class, an instance of the annotation is created and added to the 'annotations' metadata for the class.

    The parameters for the decorator factory will be the same as the parameters for the class constructor.

     import { makeDecorator } from "reflect-helper";
     var Entity = makeDecorator(EntityAnnotation);

    The Entity decorator can be applied to the class as demonstrated below. In this example we also apply a property decorator called Field:

     class Person {
         age: number;
         constructor(age: number) {
             this.age = age;

    If not using a transpiler, such as TypeScript or Babel, that supports decorators, we can instead apply the decorator using a helper function called decorate as follows:

    var decorate = require("reflect-helper").decorate;
    function Person(age) {
        this.age = age;
    decorate(Person, Entity("person"), {
        age: Field()

    Now at a later point in our program we want to check if the Person class is annotated with EntityAnnotation. We can do this using the getType function.

    import { getType } from "reflect-helper";
    var personType = getType(Person);
    console.log(personType.hasAnnotation(EntityAnnotation)); // true
    console.log(personType.getAnnotations(EntityAnnotation)[0].name); // person

    Let's say we want to get the name and type of the first annotated property on the Person class:

    var property =[0];
    console.log(; // age
    console.log(; // Number 

    Note that only properties that have an annotation are available in the [properties] array on the Type class. Also, the type of the property is only available if the --emitDecoratorMetadata compiler option is enabled in TypeScript.


    npm i reflect-helper

    DownloadsWeekly Downloads






    Last publish


    • artifacthealth