Skip to content

Parameter reflection

Retrieving an instance

Retrieve an instance from the parent method using getParameter() or getParameters(). It is not possible to manually instantiate a reflection parameter.

Properties

method: ReflectionMethod

The ReflectionMethod instance that this parameter belongs to.

index: number

The parameter index as an integer. The first parameter in a method will have the index 0.

name: string

The name of the parameter.

hasDefault: boolean

Whether or not the parameter has an ES6 default value.

isPrimitiveType: boolean

Whether or not the parameter is a primitive type such as a string, number, bigint, boolean, symbol, or undefined. Please note that this does not include generic object types due to metadata limitations.

isKnownType: boolean

Whether or not the parameter has a known and non-generic type. This will return false for generic objects, generic functions, null, and undefined.

isClassType: boolean

Whether or not the parameter accepts an instance of a class.

isReflectableType: boolean

Whether or not the parameter's accepted type is a reflectable user class.

Parameter type

getType()

Returns the parameters's expected type as it was defined by the TypeScript compiler. When type information is not available, or when the parameter does not define a type, undefined will be returned instead.

  • When a parameter accepts multiple types, this will return a generic Object
  • When a parameter accepts a class instance, this will return the class constructor

Examples

if (parameter.getType() === String) {}
if (parameter.getType() === undefined) {}

getTypeString()

Returns the parameters's expected type as a string. This will be identical to what the typeof operator would produce.

Examples

if (parameter.getTypeString() === 'string') {}
if (parameter.getTypeString() === 'undefined') {}

Retrieving attributes

This library has a powerful alternative to decorators called attributes. You can use the following methods to retrieve attribute instances that have been applied to a parameter.

getAttributes(attribute?: IAttribute<any>)

Returns an array of instances of the specified attribute on the parameter. When the attribute argument is not supplied, it will return all attributes on the parameter.

Parameters

Name Type Description Default
attribute IAttribute<any> A reference to the attribute function. undefined

Examples

for (const attribute of parameter.getAttributes(ExampleAttribute)) {
    // Do something
}

getAttribute(attribute: IAttribute<any>)

Returns an instance of the specified attribute on the parameter or undefined if not found. Only the last instance (the instance declared in the code last) will be returned.

Parameters

Name Type Description Default
attribute IAttribute<any> A reference to the attribute function. required

Examples

const latest = parameter.getAttributes(ExampleAttribute);

hasAttribute(attribute: IAttribute<any>)

Returns true if the parameter has any attributes of the specified type applied.

Parameters

Name Type Description Default
attribute IAttribute<any> A reference to the attribute function. required

Examples

if (parameter.hasAttribute(ExampleAttribute)) {
    // Do something!
}

Managing metadata

These methods use the reflect-metadata library under the hood. You can use the @Meta decorator built into this package, or refer to the TypeScript Handbook on Decorators, to set metadata on your parameters.

Warning

Parameter metadata is stored on the method in no particular format. It is therefore extremely important that you use the @Meta() or @Meta.Parameter() decorators in this package to set parameter metadata in a format that the below methods can understand.

To learn more about the format we expect, and how to write your own compatible parameter decorators, check out the parameter metadata internals guide.

getMetadata(key: any)

Returns the value of a metadata key on the parameter.

Parameters

Name Type Description Default
key any The key to look for. This must be identical to the key you originally used to store the data. required

getAllMetadata()

Returns a Map containing all metadata on the parameter.

Examples

const meta = reflect.getAllMetadata();
const value = meta.get('key');

setMetadata(key: any, value: any)

Sets the value of a metadata key on the parameter.

Parameters

Name Type Description Default
key any The key to set data for. required
value any The data to store under the key. Existing data will be overwritten. required

hasMetadata(key: any)

Returns true if this parameter has metadata matching the given key.

Parameters

Name Type Description Default
key any The key to look for. This must be identical to the key you originally used to store the data. required