【发布时间】:2019-10-15 06:59:26
【问题描述】:
我正在编写泛型方法,以便在不同的前端应用程序中使用它们,其想法是能够调用函数.postAsync<CustomModel>('www.mysite.com',..., CustomModel);,并且预期的响应是一个CustomModel对象。
我希望能够为第二个参数设置一个默认值,以便默认情况下该值将是不同的模型,并且可以在需要时被覆盖。
如何为 Constructable<T> 类型的参数设置默认值,其中接口 Constructable 表示
interface Constructable<T> { new(params : any): T ;}
我尝试设置一个带有参数的接口的默认值并将参数设置为不同的类型,但我总是收到错误Type Constructable<CustomModel> is not assignable to type Constructable<T>。我还在 CustomModel 通用方法中设置了默认类型 T ,然后尝试了这个并得到了同样的错误。
interface Constructable<T> { new(params : any): T ;}
export default class WebapiBase {
static async postAsync<T = CustomModel>(
uri: string,
body: object,
headers: CustomHeaders = new CustomHeaders(),
// This is the part giving errors
model: Constructable<T> = <Constructable<CustomModel>>,): Promise<T> {
return this.requestAsync<T>(model, HTTP_METHOD.POST, uri, headers, body);
}
private static async requestAsync<T>(
model: Constructable<T>,
method: HTTP_METHOD,
uri: string,
headers: CustomHeaders,
body?: object): Promise<T> {
const url = new URL(uri, window.location.origin);
const request = this.buildRequest(url, method, headers, body);
const bodyResponse = await fetch(request)
.then(response => this.validate(response, request.headers))
.then(validResponse => this.extractBody(validResponse))
// Here is where the response body is being used to initialise one of the custom models that we are passing in. Code below
.then(extractedBody => this.buildModel<T>(model, extractedBody))
.catch((error) => { throw this.handleError(error); });
return bodyResponse;
}
private static buildModel<T>(
Model: Constructable<T>,
params: ResponseBody,
): T {
return new Model(params);
}
}
我希望我不必将模型传递给方法postAsync(),并且它总是会返回一个 CustomModel 对象。但实际上我得到了这个错误Type Constructable<CustomModel> is not assignable to type Constructable<T>
Example in Playground, hover over Constructable in args to see error
【问题讨论】:
-
对于有问题的代码来说,minimal reproducible example 通常是一个好主意,这样有人可以直接进入 IDE 并重现问题,而不会产生无关的依赖或错误......理想情况下甚至会有一个链接到像 the Playground 这样的 Web IDE。很难提出已知可分配给泛型类型的具体类型。不能保证
CustomModel可以分配给T(default 只是一个默认值,而不是保证),所以当你需要返回一个CustomModel时返回一个类型错误T. -
那是真的,我真的很抱歉。这是一个非常复杂的例子,明天早上我会将我的问题抽象为一个最小的可重现的例子。谢谢。
-
在问题中添加了一个链接到我刚刚启动的操场示例,它不是“正在运行”,但它确实得到了相同的错误,或者至少是我在摆弄这个问题时遇到的错误之一.很快就会变得更简洁。谢谢
标签: typescript generics casting