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
undefinedif 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
ReflectionMethodinstances that describe each method on the class.Parameters
Name Type Description Default filterMethodFilter,undefinedAn optional filter to choose what kind of methods are returned. undefinedExamples// 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
ReflectionMethodinstance for a method matching the given name. Returnsundefinedwhen no matching method is found.Parameters
Name Type Description Default namestringThe name of the method to search for. required filterMethodFilter,undefinedAn optional filter to choose what kind of methods are checked. undefinedExamplesconst method = reflect.getMethod('methodName'); method?.invoke(o);
hasMethod(name: string, filter?: MethodFilter)¶
Returns
trueif the class has a method matching the given name.Parameters
Name Type Description Default namestringThe name of the method to search for. required filterMethodFilter,undefinedAn optional filter to use when checking for a matching method. undefinedExamplesreflect.hasMethod('staticMethodName') // true reflect.hasMethod('staticMethodName', MethodFilter.Local) // false
getConstructorMethod()¶
Returns a
ReflectionMethodinstance representing theconstructormethod 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
ReflectionPropertyinstances that describe each known property on the class.Parameters
Name Type Description Default filterPropertyFilter,undefinedAn optional filter to choose what kind of properties are returned. undefinedExamples// 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
ReflectionPropertyinstance for a property matching the given name. Returnsundefinedwhen no matching property is found.Parameters
Name Type Description Default namestringThe name of the property to search for. required filterPropertyFilter,undefinedAn optional filter to choose what kind of properties are checked. undefinedExamplesconst property = reflect.getProperty('propertyName'); const value = property?.getMetadata('key');
hasProperty(name: string, filter?: PropertyFilter)¶
Returns
trueif the class has a property matching the given name.Parameters
Name Type Description Default namestringThe name of the property to search for. required filterPropertyFilter,undefinedAn optional filter to use when checking for a matching property. undefinedExamplesreflect.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
attributeargument is not supplied, it will return all attributes on the class.Parameters
Name Type Description Default attributeIAttribute<any>A reference to the attribute function. undefinedExamplesfor (const attribute of reflect.getAttributes(ExampleAttribute)) { // Do something }
getAttribute(attribute: IAttribute<any>)¶
Returns an instance of the specified attribute on the class or
undefinedif not found. Only the last instance (the instance declared in the code last) will be returned.Parameters
Name Type Description Default attributeIAttribute<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 attributeIAttribute<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 keyanyThe key to look for. This must be identical to the key you originally used to store the data. required
getAllMetadata()¶
Returns a
Mapcontaining 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 keyanyThe key to set data for. required valueanyThe data to store under the key. Existing data will be overwritten. required
hasMetadata(key: any)¶
Returns
trueif this class has metadata matching the given key.Parameters
Name Type Description Default keyanyThe 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
ReflectionClassinstances 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();hierarchyvariable will be an array that looks like the following.[ ReflectionClass(Vehicle), ReflectionClass(Car), ReflectionClass(Hatchback) ]
hasType(type: Type<any>)¶
Returns
trueif this class has a class within its hierarchy, including itself, which matches the given class constructor type.Parameters
Name Type Description Default typeType<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
trueif this class has a class within its hierarchy, not including itself, which matches the given class constructor type.Parameters
Name Type Description Default typeType<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 argsany[]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]);