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 object
T
,null
,undefined
An instance of the class to invoke the method on. For static methods, pass null
orundefined
.undefined
Examplesconst 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 object
T
,null
,undefined
An instance of the class to invoke the method on. For static methods, pass null
orundefined
.undefined
...args
any[]
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
ReflectionParameter
instances which describe each parameter from the method.Parameters
Name Type Description Default filter
ParameterFilter
,undefined
An optional filter to choose what kind of parameters are returned. undefined
Examples// 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
ReflectionParameter
instance for the specified parameter. Returnsundefined
if there is no matching parameter.Parameters
Name Type Description Default name
string
The name of the parameter to search for. required filter
ParameterFilter
,undefined
An optional filter to choose what kind of parameters are checked. undefined
Examplesconst param = method.getParameter('paramName');
getParameter(index: number)
¶
Returns a
ReflectionParameter
instance for the parameter at the specified index. Returnsundefined
if there is no matching parameter.Parameters
Name Type Description Default index
number
The index to retrieve (zero-based). required Examplesconst param = method.getParameter(0);
hasParameter(name: string, filter?: ParameterFilter)
¶
Returns
true
if the method contains a parameter with the specified name, optionally with a filter applied.Parameters
Name Type Description Default name
string
The name of the parameter to search for. required filter
ParameterFilter
,undefined
An optional filter to choose what kind of parameters are checked. undefined
Examplesif (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,
undefined
will 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
typeof
operator 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
attribute
argument is not supplied, it will return all attributes on the method.Parameters
Name Type Description Default attribute
IAttribute<any>
A reference to the attribute function. undefined
Examplesfor (const attribute of method.getAttributes(ExampleAttribute)) { // Do something }
getAttribute(attribute: IAttribute<any>)
¶
Returns an instance of the specified attribute on the method 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 = 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 attribute
IAttribute<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 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 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 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 method 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