Class reflection¶
Creating an instance¶
To get started with reflection, create a ReflectionClass
instance and pass a reference to a class constructor. You can also pass an instance of a class and the constructor will be inferred.
const reflect = new ReflectionClass(ExampleClass);
const reflect = new ReflectionClass(new ExampleClass());
Properties¶
name: string
¶
The name of the class as a string.
parent: ReflectionClass?
¶
The reflection instance for the parent class or
undefined
if the class has no parent.
target: Type<T>
¶
A reference to the underlying class constructor.
prototype: Object
¶
A reference to the underlying class prototype.
Retrieving methods¶
getMethods(filter?: MethodFilter)
¶
Returns an array of
ReflectionMethod
instances that describe each method on the class.Parameters
Name Type Description Default filter
MethodFilter
,undefined
An optional filter to choose what kind of methods are returned. undefined
Examples// Iterate over all methods for (const method of reflect.getMethods()) {}
// Filter static methods const staticMethods = reflect.getMethods(MethodFilter.Static);
// Multiple filters (AND) const staticOwnMethods = reflect.getMethods( MethodFilter.Static | MethodFilter.Own );
getMethod(name: string, filter?: MethodFilter)
¶
Returns the
ReflectionMethod
instance for a method matching the given name. Returnsundefined
when no matching method is found.Parameters
Name Type Description Default name
string
The name of the method to search for. required filter
MethodFilter
,undefined
An optional filter to choose what kind of methods are checked. undefined
Examplesconst method = reflect.getMethod('methodName'); method?.invoke(o);
hasMethod(name: string, filter?: MethodFilter)
¶
Returns
true
if the class has a method matching the given name.Parameters
Name Type Description Default name
string
The name of the method to search for. required filter
MethodFilter
,undefined
An optional filter to use when checking for a matching method. undefined
Examplesreflect.hasMethod('staticMethodName') // true reflect.hasMethod('staticMethodName', MethodFilter.Local) // false
getConstructorMethod()
¶
Returns a
ReflectionMethod
instance representing theconstructor
method for the class. The constructor method is guaranteed to exist, even if one is not explicitly defined.Examples// These return the same exact object reflect.getConstructorMethod(); reflect.getMethod('constructor');
Retrieving properties¶
Warning
Due to the way property metadata is stored, properties will not be visible to reflection unless you use the @Meta()
or @Meta.Property()
decorators in some way.
This is because, while property metadata is stored in the same way as methods, there is no way to iterate over properties on the prototype like you can with methods. This means there's no way for reflection to identify what properties exist.
To get around this problem, the @Meta
decorators store a set of known property names on the class under the reflection:properties
key.
getProperties(filter?: PropertyFilter)
¶
Returns an array of
ReflectionProperty
instances that describe each known property on the class.Parameters
Name Type Description Default filter
PropertyFilter
,undefined
An optional filter to choose what kind of properties are returned. undefined
Examples// Iterate over all properties for (const property of reflect.getProperties()) {}
// Filter own properties (not from a parent class) const ownProperties = reflect.getProperties(PropertyFilter.Own);
getProperty(name: string, filter?: PropertyFilter)
¶
Returns the
ReflectionProperty
instance for a property matching the given name. Returnsundefined
when no matching property is found.Parameters
Name Type Description Default name
string
The name of the property to search for. required filter
PropertyFilter
,undefined
An optional filter to choose what kind of properties are checked. undefined
Examplesconst property = reflect.getProperty('propertyName'); const value = property?.getMetadata('key');
hasProperty(name: string, filter?: PropertyFilter)
¶
Returns
true
if the class has a property matching the given name.Parameters
Name Type Description Default name
string
The name of the property to search for. required filter
PropertyFilter
,undefined
An optional filter to use when checking for a matching property. undefined
Examplesreflect.hasProperty('propertyName')
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 class.
getAttributes(attribute?: IAttribute<any>)
¶
Returns an array of instances of the specified attribute on the class. When the
attribute
argument is not supplied, it will return all attributes on the class.Parameters
Name Type Description Default attribute
IAttribute<any>
A reference to the attribute function. undefined
Examplesfor (const attribute of reflect.getAttributes(ExampleAttribute)) { // Do something }
getAttribute(attribute: IAttribute<any>)
¶
Returns an instance of the specified attribute on the class 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 = reflect.getAttributes(ExampleAttribute);
hasAttribute(attribute: IAttribute<any>)
¶
Returns true if the class has any attributes of the specified type applied.
Parameters
Name Type Description Default attribute
IAttribute<any>
A reference to the attribute function. required Examplesif (reflect.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 classes.
getMetadata(key: any)
¶
Returns the value of a metadata key on the class.
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 class.Examplesconst meta = reflect.getAllMetadata(); const value = meta.get('key');
setMetadata(key: any, value: any)
¶
Sets the value of a metadata key on the class.
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 class 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
Ancestry¶
getHierarchy()
¶
Returns an array of
ReflectionClass
instances representing the hierarchy of the current class. The first element in the array will be the topmost parent class, and the last element will be the current class.For classes with no parents, this will return an array containing only the current class.
ExamplesThe aboveclass Vehicle {} class Car extends Vehicle {} class Hatchback extends Car {} const reflection = new ReflectionClass(Hatchback); const hierarchy = reflection.getHierachy();
hierarchy
variable will be an array that looks like the following.[ ReflectionClass(Vehicle), ReflectionClass(Car), ReflectionClass(Hatchback) ]
hasType(type: Type<any>)
¶
Returns
true
if this class has a class within its hierarchy, including itself, which matches the given class constructor type.Parameters
Name Type Description Default type
Type<any>
A reference to the class to check against. required Examplesclass Vehicle {} class Car extends Vehicle {} class Hatchback extends Car {} const reflection = new ReflectionClass(Hatchback); reflection.hasType(Car) // true reflection.hasType(Vehicle) // true reflection.hasType(Hatchback) // true - includes itself
hasAncestorType(type: Type<any>)
¶
Returns
true
if this class has a class within its hierarchy, not including itself, which matches the given class constructor type.Parameters
Name Type Description Default type
Type<any>
A reference to the class to check against. required Examplesclass Vehicle {} class Car extends Vehicle {} class Hatchback extends Car {} const reflection = new ReflectionClass(Hatchback); reflection.hasAncestorType(Car) // true reflection.hasAncestorType(Vehicle) // true reflection.hasAncestorType(Hatchback) // false - doesn't include itself
Creating instances¶
create(args?: any[])
¶
Creates a new instance of the class with the given constructor arguments.
Parameters
Name Type Description Default args
any[]
An array of constructor arguments. []
Examplesclass Hatchback { public constructor(public name: string, public year: number) { } } const reflection = new ReflectionClass(Hatchback); const instance = reflection.create(['Honda Civic', 2020]);