gpt4 book ai didi

angular - 回调中的 Typescript 泛型

转载 作者:太空狗 更新时间:2023-10-29 17:15:35 27 4
gpt4 key购买 nike

我正在尝试制作一个通用服务,该服务将获取一些远程数据并使用它创建一些对象。

@Injectable()
export class tService<T> {
private _data: BehaviorSubject<T[]> = new BehaviorSubject([]);
private url = '...url...'; // URL to web api

constructor(private http: HttpClient) {
this.http.get<T[]>(this.url).subscribe(theThings => {
theThings = _.map(theThings, (theThing) => new T(theThing));
this._data.next(theThings);
});
}
}

这给了我错误T only refers to type, but is being used as a value here。好没问题。我明白发生了什么。我看到有几个问题询问类似的事情。

例如:

似乎我遇到的任何解决方案要么在某个时候在类中进行硬编码,要么在某处添加 T 的类构造函数。但我不知道该怎么做。我的问题是被实例化的类在构造函数中有参数,而我使用的是 Angular 的 DI 系统,所以我不能(???)向服务的构造函数添加参数。

constructor(private playersService: gService<Player>, private alliesService: gService<Ally>) { }

谁能知道我应该在这里做什么?另外,如果可能的话,我更希望答案不是某种“黑客”。如果要在做一些几乎不可读的事情和复制并粘贴服务几次并更改它所指的类之间做出选择,我会选择第二个选项。

最佳答案

类名既指类的类型,也指类的构造函数,这就是为什么您可以同时编写 let x: ClassNamenew ClasName() .通用参数只是一种类型(例如,很像接口(interface)),这就是编译器提示您将其用作值(预期的值是构造函数)的原因。您需要做的是传递一个额外的参数,该参数将成为 T 的构造函数:

export class tService<T> {
private _data: BehaviorSubject<T[]> = new BehaviorSubject<T>([]);
private url = '...url...'; // URL to web api

constructor(private http: HttpClient, ctor: new (data: Partial<T>)=> T) {
this.http.get<T[]>(this.url).subscribe(theThings => {
theThings = _.map(theThings, (theThing) => new ctor(theThing));
this._data.next(theThings);
});
}
}
//Usage
class MyClass {
constructor(p: Partial<MyClass>) {
// ...
}
}
new tService(http, MyClass)

注意构造函数签名的参数可能因您的用例而异。

编辑

您提到您不能向构造函数添加参数,当我们运行代码时,泛型(以及一般的所有类型)都会被删除,因此您不能在运行时依赖 T,在某个地方,有人将不得不传入类构造函数。您可以通过多种方式做到这一点,最简单的版本是为每个 T 实例创建专用类,然后注入(inject)特定类:

class tServiceForMyClass extends tService<MyClass> {
constructor(http: HttpClient) {
super(http, MyClass)
}
}

或者您可以在需要构造函数作为参数的 init 方法中完成依赖于构造函数的工作:

export class tService<T> {
private _data: BehaviorSubject<T[]> = new BehaviorSubject<T>();
private url = '...url...'; // URL to web api

constructor(private http: HttpClient){}
init(ctor: new (data: Partial<T>)=> T) {
this.http.get<T[]>(this.url).subscribe(theThings => {
theThings = _.map(theThings, (theThing) => new ctor(theThing));
this._data.next(theThings);
});
}
}

关于angular - 回调中的 Typescript 泛型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49664919/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com