# Metadata Class

Module
import { Metadata } from "@tsed/core"
Source/packages/core/src/class/Metadata.ts

# Overview

class Metadata {
    static get(key: string, target: any, propertyKey?: string | symbol): any;
    static getOwn(key: string, target: any, propertyKey?: string | symbol): any;
    static getType(target: any, propertyKey?: string | symbol): any;
    static getOwnType(target: any, propertyKey?: string | symbol): any;
    static getReturnType(target: any, propertyKey?: string | symbol): any;
    static getOwnReturnType(target: any, propertyKey?: string | symbol): any;
    static has(key: string, target: any, propertyKey?: string | symbol): boolean;
    static hasOwn(key: string, target: any, propertyKey?: string | symbol): boolean;
    static delete(key: string, target: any, propertyKey?: string | symbol): boolean;
    static setParamTypes(target: any, propertyKey: string | symbol, value: any): void;
    static getTargetsFromPropertyKey: (metadataKey: string | symbol) => any[];
    static set(key: string, value: any, target: any, propertyKey?: string | symbol): void;
    static getParamTypes(targetPrototype: any, propertyKey?: string | symbol): any[];
    static getOwnParamTypes(target: any, propertyKey?: string | symbol): any[];
}

# Members

static get(key: string, target: any, propertyKey?: string | symbol): any
Param Type Description
key string A key used to store and retrieve metadata. target

Gets the metadata value for the provided metadata key on the target object or its prototype chain.

class Example {
    // property declarations are not part of ES6, though they are valid in TypeScript:
    // static staticProperty;
    // property;

    static staticMethod(p) { }
    method(p) { }
}

// constructor
result = Metadata.get("custom:annotation", Example);

// property (on constructor)
result = Metadata.get("custom:annotation", Example, "staticProperty");

// property (on prototype)
result = Metadata.get("custom:annotation", Example.prototype, "property");

// method (on constructor)
result = Metadata.get("custom:annotation", Example, "staticMethod");

// method (on prototype)
result = Metadata.get("custom:annotation", Example.prototype, "method");
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

static getOwn(key: string, target: any, propertyKey?: string | symbol): any
Param Type Description
key string A key used to store and retrieve metadata. target

Gets the metadata value for the provided metadata key on the target object or its prototype chain.

class Example {
    // property declarations are not part of ES6, though they are valid in TypeScript:
    // static staticProperty;
    // property;

    static staticMethod(p) { }
    method(p) { }
}

// constructor
result = Metadata.getOwn("custom:annotation", Example);

// property (on constructor)
result = Metadata.getOwn("custom:annotation", Example, "staticProperty");

// property (on prototype)
result = Metadata.getOwn("custom:annotation", Example.prototype, "property");

// method (on constructor)
result = Metadata.getOwn("custom:annotation", Example, "staticMethod");

// method (on prototype)
result = Metadata.getOwn("custom:annotation", Example.prototype, "method");
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

static getType(target: any, propertyKey?: string | symbol): any
Param Type Description
target any The target object on which the metadata is defined. propertyKey

Gets the metadata value for the provided metadata DESIGN_TYPE on the target object or its prototype chain.

class Example {
    // property declarations are not part of ES6, though they are valid in TypeScript:
    // static staticProperty;
    // property;

    static staticMethod(p) { }
    method(p) { }
}

// on contructor
result = Metadata.getType(Example);

// property (on constructor)
result = Metadata.getType(Example, "staticProperty");

// method (on constructor)
result = Metadata.getType(Example, "staticMethod");
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

static getOwnType(target: any, propertyKey?: string | symbol): any
Param Type Description
target any The target object on which the metadata is defined. propertyKey

Gets the metadata value for the provided metadata DESIGN_TYPE on the target object or its prototype chain.

class Example {
    // property declarations are not part of ES6, though they are valid in TypeScript:
    // static staticProperty;
    // property;

    static staticMethod(p) { }
    method(p) { }
}

// on contructor
result = Metadata.getOwnType(Example);

// property (on constructor)
result = Metadata.getOwnType(Example, "staticProperty");

// method (on constructor)
result = Metadata.getOwnType(Example, "staticMethod");
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

static getReturnType(target: any, propertyKey?: string | symbol): any
Param Type Description
target any The target object on which the metadata is defined. propertyKey

Gets the metadata value for the provided metadata DESIGN_RETURN_TYPE on the target object or its prototype chain.

class Example {
    // property declarations are not part of ES6, though they are valid in TypeScript:
    // static staticProperty;
    // property;

    static staticMethod(p) { }
    method(p) { }
}

// on contructor
result = Metadata.getReturnType(Example);

// property (on constructor)
result = Metadata.getReturnType(Example, "staticProperty");

// method (on constructor)
result = Metadata.getReturnType(Example, "staticMethod");
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

static getOwnReturnType(target: any, propertyKey?: string | symbol): any
Param Type Description
target any The target object on which the metadata is defined. propertyKey

Gets the metadata value for the provided metadata DESIGN_RETURN_TYPE on the target object or its prototype chain.

class Example {
    // property declarations are not part of ES6, though they are valid in TypeScript:
    // static staticProperty;
    // property;

    static staticMethod(p) { }
    method(p) { }
}

// on contructor
result = Metadata.getOwnReturnType(Example);

// property (on constructor)
result = Metadata.getOwnReturnType(Example, "staticProperty");

// method (on constructor)
result = Metadata.getOwnReturnType(Example, "staticMethod");
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

static has(key: string, target: any, propertyKey?: string | symbol): boolean
Param Type Description
key string A key used to store and retrieve metadata. target

Gets a value indicating whether the target object or its prototype chain has the provided metadata key defined.

class Example {
    // property declarations are not part of ES6, though they are valid in TypeScript:
    // static staticProperty;
    // property;

    static staticMethod(p) { }
    method(p) { }
}

// constructor
result = Metadata.has("custom:annotation", Example);

// property (on constructor)
result = Metadata.has("custom:annotation", Example, "staticProperty");

// property (on prototype)
result = Metadata.has("custom:annotation", Example.prototype, "property");

// method (on constructor)
result = Metadata.has("custom:annotation", Example, "staticMethod");

// method (on prototype)
result = Metadata.has("custom:annotation", Example.prototype, "method");
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

static hasOwn(key: string, target: any, propertyKey?: string | symbol): boolean
Param Type Description
key string A key used to store and retrieve metadata. target

Gets a value indicating whether the target object or its prototype chain has the provided metadata key defined.

class Example {
    // property declarations are not part of ES6, though they are valid in TypeScript:
    // static staticProperty;
    // property;

    static staticMethod(p) { }
    method(p) { }
}

// constructor
result = Metadata.has("custom:annotation", Example);

// property (on constructor)
result = Metadata.hasOwn("custom:annotation", Example, "staticProperty");

// property (on prototype)
result = Metadata.hasOwn("custom:annotation", Example.prototype, "property");

// method (on constructor)
result = Metadata.hasOwn("custom:annotation", Example, "staticMethod");

// method (on prototype)
result = Metadata.hasOwn("custom:annotation", Example.prototype, "method");
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

static delete(key: string, target: any, propertyKey?: string | symbol): boolean
Param Type Description
key string A key used to store and retrieve metadata. target

Deletes the metadata entry from the target object with the provided key.

class Example {
    // property declarations are not part of ES6, though they are valid in TypeScript:
    // static staticProperty;
    // property;

    static staticMethod(p) { }
    method(p) { }
}

// constructor
result = Metadata.delete("custom:annotation", Example);

// property (on constructor)
result = Metadata.delete("custom:annotation", Example, "staticProperty");

// property (on prototype)
result = Metadata.delete("custom:annotation", Example.prototype, "property");

// method (on constructor)
result = Metadata.delete("custom:annotation", Example, "staticMethod");

// method (on prototype)
result = Metadata.delete("custom:annotation", Example.prototype, "method");
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

static setParamTypes(target: any, propertyKey: string | symbol, value: any): void
Param Type Description
target any The target object on which the metadata is defined. propertyKey

Set the metadata value for the provided metadata DESIGN_PARAM_TYPES on the target object or its prototype chain.

class Example {
    // property declarations are not part of ES6, though they are valid in TypeScript:
    // static staticProperty;
    // property;

    static staticMethod(p) { }
    method(p) { }
}

// on contructor
result = Metadata.setParamTypes(Example, undefined, [Object]);

// property (on constructor)
result = Metadata.setParamTypes(Example, "staticProperty", [Object]);

// property (on prototype)
result = Metadata.setParamTypes(Example.prototype, "property", [Object]);

// method (on constructor)
result = Metadata.setParamTypes(Example, "staticMethod", [Object]);

// method (on prototype)
result = Metadata.setParamTypes(Example.prototype, "method", [Object]);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

static getTargetsFromPropertyKey: (metadataKey: string | symbol) => any[]

Get all metadata for a metadataKey.


static set(key: string, value: any, target: any, propertyKey?: string | symbol): void
Param Type Description
key string A key used to store and retrieve metadata. value

Define a unique metadata entry on the target.

class Example {
    // property declarations are not part of ES6, though they are valid in TypeScript:
    // static staticProperty;
    // property;

    static staticMethod(p) { }
    method(p) { }
}

// constructor
Reflect.defineMetadata("custom:annotation", options, Example);

// property (on constructor)
Reflect.defineMetadata("custom:annotation", Number, Example, "staticProperty");

// property (on prototype)
Reflect.defineMetadata("custom:annotation", Number, Example.prototype, "property");

// method (on constructor)
Reflect.defineMetadata("custom:annotation", Number, Example, "staticMethod");

// method (on prototype)
Reflect.defineMetadata("custom:annotation", Number, Example.prototype, "method");

// decorator factory as metadata-producing annotation.
function MyAnnotation(options): PropertyDecorator {
    return (target, key) => Reflect.defineMetadata("custom:annotation", options, target, key);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28

static getParamTypes(targetPrototype: any, propertyKey?: string | symbol): any[]
Param Type Description
propertyKey string | symbol Optional. The property key for the target.

Gets the metadata value for the provided metadata DESIGN_PARAM_TYPES on the target object or its prototype chain.

class Example {
    // property declarations are not part of ES6, though they are valid in TypeScript:
    // static staticProperty;
    // property;

    static staticMethod(p) { }
    method(p) { }
}

// on contructor
result = Metadata.getParamTypes(Example.prototype);

// property (on constructor)
result = Metadata.getParamTypes(Example.prototype, "staticProperty");

// method (on constructor)
result = Metadata.getParamTypes(Example.prototype, "staticMethod");
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

static getOwnParamTypes(target: any, propertyKey?: string | symbol): any[]
Param Type Description
target any The target object on which the metadata is defined. propertyKey

Gets the metadata value for the provided metadata DESIGN_PARAM_TYPES on the target object or its prototype chain.

class Example {
    // property declarations are not part of ES6, though they are valid in TypeScript:
    // static staticProperty;
    // property;

    static staticMethod(p) { }
    method(p) { }
}

// on contructor
result = Metadata.getParamTypes(Example);

// property (on constructor)
result = Metadata.getParamTypes(Example, "staticProperty");

// method (on constructor)
result = Metadata.getParamTypes(Example, "staticMethod");
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17