本文介绍了是否有可能获得泛型类型的类型名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个方法签名 execute< TResult>():Observable< TResult>
我得到TResult类型的名称?
示例:
execute< ViewModel>
- >ViewModel是我所需要的结果。
声明:
执行< TResult>(ctor: {new():TResult}):< TResult> {
console.log(ctor.name)//打印SomeClass
return< any> null;
}
用法:
执行< SomeClass>(SomeClass);
I have a method signature of execute<TResult>(): Observable<TResult>
How do I get the name of the TResult type?
Example:
execute<ViewModel>
--> "ViewModel" is the result I need.
解决方案
As far as I know it is not possible to get the name of TResult
, but if you provide the accordingly constructor function you can get the name.
Declaration:
execute<TResult>(ctor: { new (): TResult }) : <TResult> {
console.log(ctor.name) //Prints out SomeClass
return <any>null;
}
Usage:
execute<SomeClass>(SomeClass);
这篇关于是否有可能获得泛型类型的类型名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!