是否有可能获得泛型类型的类型名称? [英] Is it possible to get the type name of a generic type?

查看:171
本文介绍了是否有可能获得泛型类型的类型名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个方法签名 execute< TResult>():Observable< TResult>



我得到TResult类型的名称?



示例:

execute< ViewModel> - >ViewModel是我所需要的结果。

据我所知,不可能得到 TResult 的名字,但是如果你提供相应的构造函数,你可以得到这个名字。


$ b 声明:

 执行< 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);

这篇关于是否有可能获得泛型类型的类型名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆