Skip to content

Meta Decorator

This is a "smart decorator" that can be used to set metadata on classes, methods, and parameters. You can also build upon this decorator to easily create your own metadata decorators without any of the associated complexity.

Variations

@Meta(key: any, value: any)

Returns a metadata decorator that can be applied to classes, methods, and parameters. This is the one you'll want to use in practice – the rest are mainly for building upon.

Examples

@Meta('key', 'value')
export class Example {

    @Meta('key', 'value')
    public method(@Meta('key', 'value') param: any) {

    }

}

@Meta.Class(key: any, value: any)

Returns a metadata decorator that can be applied to only classes.

Examples

@Meta.Class('key', 'value')
export class Example {

}

@Meta.Method(key: any, value: any)

Returns a metadata decorator that can be applied to only methods.

Examples

export class Example {

    @Meta.Method('key', 'value')
    public method() {

    }

}

@Meta.Property(key: any, value: any)

Returns a metadata decorator that can be applied to only properties.

Examples

export class Example {

    @Meta.Property('key', 'value')
    public prop: string;

}

@Meta.Parameter(key: any, value: any)

Returns a metadata decorator that can be applied to only parameters.

Examples

export class Example {

    public method(@Meta.Parameter('key', 'value') param: any) {

    }

}

Custom decorators

You can create a custom meta decorator using the existing @Meta decorators as a basis.

export const SmartDecorator = Meta('key', 'value');
export const ClassDecorator = Meta.Class('key', 'value');
export const MethodDecorator = Meta.Method('key', 'value');
export const ParameterDecorator = Meta.Parameter('key', 'value');

You can also create a decorator that accepts parameters:

export const VariableDecorator = (value: string) => Meta('key', value);

You can then use these constants like normal decorators.

@SmartDecorator
@ClassDecorator
@VariableDecorator('value')
export class Example {

    @MethodDecorator
    @VariableDecorator('another value')
    public method(@ParameterDecorator param: any) {

    }

}