Angular 如何在Typescript中获取泛型方法中的T类型?

Angular 如何在Typescript中获取泛型方法中的T类型?,angular,typescript,generics,Angular,Typescript,Generics,我在一个类中有一个泛型方法 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.ac

我在一个类中有一个泛型方法

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');
    }
}
“T”仅指类型,但在此处用作值

编辑:


我试图获取调用泛型方法的类的类型。例如:
getAll
我想在该方法中获取类型
SubscriberData


如何执行此操作?

您可以访问类装饰器中的类的构造函数引用、属性(或访问器)装饰器中的属性或参数装饰器中的参数(使用)

不幸的是,泛型类型参数在运行时不可用。通过这种方式,它们总是会产生一个简单的
对象
类型的运行时等价物

相反,您可以提供构造函数引用,也可以使用构造函数引用推断泛型类型(即,您不指定泛型类型,而是指定该泛型类型的相应构造函数引用):

导出类基本服务{
公共getAll(TCtor:new(…args:any[])=>T):可观察{
//获得T型
常数类型=类型(TCtor);
// ...
}
}
然后像这样使用它:

new BaseService().getAll(DataClass); // instead of 'getAll<DataClass>()'
newbaseservice().getAll(DataClass);//而不是“getAll()”


类型
new(…args:any[])=>T
只是说:一个可更新的类型(即类/构造函数),返回泛型
T
类型(换句话说,泛型
T
实例类型的对应类/构造函数)。

类型系统在编译时丢失,所以它在运行时不可用,我正在尝试获取调用泛型方法的类的类型
For Ex:getAll
我想在该方法中获取类型
SubscriberData
。用于什么目的?将更多参数推送到http调用并通过基于类型检查此条件来更改http url。
const type = typeof(T); 
export class BaseService {
    public getAll<T>(TCtor: new (...args: any[]) => T): Observable<T> {
        // get type of T
        const type = typeof(TCtor);

        // ...
    }
}
new BaseService().getAll(DataClass); // instead of 'getAll<DataClass>()'