Property reflection¶
Retrieving an instance¶
Retrieve an instance from the parent class using getProperty()
or getProperties()
. It is not possible to manually instantiate a reflection property.
Properties¶
name: string
¶
The name of the property as a string.
class: ReflectionClass
¶
The class that this property belongs to.
isTyped: boolean
¶
Whether or not the property has type metadata available from TypeScript.
Property type¶
getType()
¶
Returns the property's expected type as it was defined by the TypeScript compiler. When type information is not available, or when the property does not define a type,
undefined
will be returned instead.
- When a property accepts multiple types, this will return a generic
Object
- When a property accepts a class instance, this will return the class constructor
Examplesif (property.getType() === String) {} if (property.getType() === undefined) {}
getTypeString()
¶
Returns the property's expected type as a string. This will be identical to what the
typeof
operator would produce.Examplesif (property.getTypeString() === 'string') {} if (property.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 property.
getAttributes(attribute?: IAttribute<any>)
¶
Returns an array of instances of the specified attribute on the property. When the
attribute
argument is not supplied, it will return all attributes on the property.Parameters
Name Type Description Default attribute
IAttribute<any>
A reference to the attribute function. undefined
Examplesfor (const attribute of property.getAttributes(ExampleAttribute)) { // Do something }
getAttribute(attribute: IAttribute<any>)
¶
Returns an instance of the specified attribute on the property 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 Examplesconst latest = property.getAttributes(ExampleAttribute);
hasAttribute(attribute: IAttribute<any>)
¶
Returns true if the property has any attributes of the specified type applied.
Parameters
Name Type Description Default attribute
IAttribute<any>
A reference to the attribute function. required Examplesif (property.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 properties.
getMetadata(key: any)
¶
Returns the value of a metadata key on the property.
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 property.Examplesconst meta = reflect.getAllMetadata(); const value = meta.get('key');
setMetadata(key: any, value: any)
¶
Sets the value of a metadata key on the property.
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 property 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