I have the following function:
function test(): number {
return 42;
}
I can obtain the type of the function by using typeof
If the function in question is a method of a user defined class, you can use method decorators in conjuction with Reflect Metadata to determine the return type (constructor function) at runtime (and with it, do as you see fit).
For example, you could log it to the console:
function logReturnType(
target: Object | Function,
key: string,
descriptor: PropertyDescriptor
): PropertyDescriptor | void {
var returnType = Reflect.getMetadata("design:returntype", target, key);
console.log(returnType);
}
Just snap this method decorator on a method of your choice and you have the exact reference to the constructor function of the object that is supposedly returned from the method call.
class TestClass {
@logReturnType // logs Number (a string representation)
public test(): number {
return 42;
}
}
There are a few notable limitations to this approach, however:
Reflect.getMetadata,Also, you'll need to specify the following command line arguments for the typescript compiler, because both decorators and reflect metadata are experimental features as of writing this post:
--emitDecoratorMetadata --experimentalDecorators