【发布时间】:2018-11-06 02:29:16
【问题描述】:
我在一个类中有一个泛型方法。,
export class BaseService {
public getAll<T>(): Observable<T> {
// get type of T
// const type = typeof(T); // Tried this but getting compilation exceptions
return this.http.get<T>(this.actionUrl + 'getAll');
}
}
我将从其他几个打字稿类中调用如下方法。
this.service.getAll<SubscriberData>().subscribe(response => {
// Response
}, error => {
console.log(error);
}, () => {
// do something commonly
});
当我尝试这个得到以下异常时
const type = typeof(T);
'T' 仅指一种类型,但在这里用作值。
编辑:
我正在尝试获取调用泛型方法的类的类型。例如:getAll<SubscriberData> 我想在该方法中获取类型 SubscriberData。
我该怎么做?
【问题讨论】:
-
类型系统在编译时丢失,所以运行时不可用stackoverflow.com/questions/24469856/…
-
我正在尝试获取调用泛型方法的类的类型。
For Ex: getAll<SubscriberData>我想在该方法中获取SubscriberData类型。 -
为了什么目的?
-
将更多参数推送到 http 调用并通过基于类型检查此条件来更改 http url。
标签: angular typescript generics