Method reflection¶
Retrieving an instance¶
Retrieve an instance from the parent class using getMethod() or getMethods(). It is not possible to manually instantiate a reflection method.
Properties¶
name: string¶
The name of the method as a string.
class: ReflectionClass¶
The class that this method belongs to.
isConstructor: boolean¶
Whether or not the method is the constructor for the class.
isStatic: boolean¶
Whether or not the method is static.
isTyped: boolean¶
Whether or not the method has type metadata available from TypeScript.
prototype: Object¶
A reference to the underlying class prototype.
target: Function¶
A reference to the method's function on the class prototype.
Invoking methods¶
getClosure(object?: T | null)¶
Returns an anonymous function that invokes the method on the given object.
Parameters
Name Type Description Default objectT,null,undefinedAn instance of the class to invoke the method on. For static methods, pass nullorundefined.undefinedExamplesconst fn = method.getClosure(instance); fn(...args);
invoke(object?: T | null, ...args: any[])¶
Invokes the method on the given object with optional arguments.
Parameters
Name Type Description Default objectT,null,undefinedAn instance of the class to invoke the method on. For static methods, pass nullorundefined.undefined...argsany[]Optional arguments to pass to the method. Examplesmethod.invoke(instance);method.invoke(instance, 'Arguments here!', true);method.invoke(null, 'Static methods');
Managing parameters¶
getParameters(filter?: ParameterFilter)¶
Returns an array of
ReflectionParameterinstances which describe each parameter from the method.Parameters
Name Type Description Default filterParameterFilter,undefinedAn optional filter to choose what kind of parameters are returned. undefinedExamples// Iterate over all parameters for (const param of reflect.getParameters()) {}// Filter parameters that don't have default values const requiredParams = reflect.getParameters( ParameterFilter.WithoutDefault );// Multiple filters (AND) // This will find all parameters with a non-generic type that have metadata const knownNonPrimitives = reflect.getMethods( ParameterFilter.Meta | ParameterFilter.NonPrimitiveType );
getParameter(name: string, filter?: ParameterFilter)¶
Returns a
ReflectionParameterinstance for the specified parameter. Returnsundefinedif there is no matching parameter.Parameters
Name Type Description Default namestringThe name of the parameter to search for. required filterParameterFilter,undefinedAn optional filter to choose what kind of parameters are checked. undefinedExamplesconst param = method.getParameter('paramName');
getParameter(index: number)¶
Returns a
ReflectionParameterinstance for the parameter at the specified index. Returnsundefinedif there is no matching parameter.Parameters
Name Type Description Default indexnumberThe index to retrieve (zero-based). required Examplesconst param = method.getParameter(0);
hasParameter(name: string, filter?: ParameterFilter)¶
Returns
trueif the method contains a parameter with the specified name, optionally with a filter applied.Parameters
Name Type Description Default namestringThe name of the parameter to search for. required filterParameterFilter,undefinedAn optional filter to choose what kind of parameters are checked. undefinedExamplesif (method.hasParameter('paramName')) { // ... }
Return types¶
getReturnType()¶
Returns the method's return type as it was defined by the TypeScript compiler. When type information is not available, or when the method does not return anything,
undefinedwill be returned instead.
- When a function can return multiple types, this will return a generic
Object- When a function returns a class instance, this will return the class constructor
Examplesif (method.getReturnType() === String) { const str = method.invoke(o); }
getReturnTypeString()¶
Returns the method's return type as a string. This will be identical to what the
typeofoperator would produce.Examplesif (method.getReturnTypeString() === 'string') { const str = method.invoke(o); }
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 method.
getAttributes(attribute?: IAttribute<any>)¶
Returns an array of instances of the specified attribute on the method. When the
attributeargument is not supplied, it will return all attributes on the method.Parameters
Name Type Description Default attributeIAttribute<any>A reference to the attribute function. undefinedExamplesfor (const attribute of method.getAttributes(ExampleAttribute)) { // Do something }
getAttribute(attribute: IAttribute<any>)¶
Returns an instance of the specified attribute on the method 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 = method.getAttributes(ExampleAttribute);
hasAttribute(attribute: IAttribute<any>)¶
Returns true if the method has any attributes of the specified type applied.
Parameters
Name Type Description Default attributeIAttribute<any>A reference to the attribute function. required Examplesif (method.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 methods.
getMetadata(key: any)¶
Returns the value of a metadata key on the method.
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 method.Examplesconst meta = reflect.getAllMetadata(); const value = meta.get('key');
setMetadata(key: any, value: any)¶
Sets the value of a metadata key on the method.
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 method 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