我刚刚在Visual Studio 2015中使用TypeScript在我的Aurelia CLI项目上做了npm update
。我正在使用aurelia-fetch-client
来调用我的Web API(.NET Core)后端。
这是以前编译并正常运行的代码示例:
import { autoinject } from "aurelia-framework";
import { HttpClient, json } from "aurelia-fetch-client";
import { SupportedCultureInfo } from "../../model/Resources/SupportedCultureInfo";
@autoinject()
export class ResourceService {
constructor(private http: HttpClient) {
}
getSupportedCultures(): Promise<SupportedCultureInfo[]> {
return this.http.fetch("resource/cultures").then(response => response.json());
}
}
Visual Studio和ReSharper都没有在代码编辑器UI中提供任何不能编译的指示,但是在最近的更新之后,我的构建现在因此错误而被破坏:
TS2322: Type 'Promise<Response>' is not assignable to type 'Promise<SupportedCultureInfo[]>'
到目前为止,我发现的唯一解决方法是返回Promise<any>
。我真正想要做的是从JSON结果映射的返回类,并且方法的返回类型也是一个强类型的Promise。
是否有人知道最近可能导致此变化的内容?这非常令人沮丧。
更新:
这是我能够使用的代码:
import { autoinject } from "aurelia-framework";
import { HttpClient, json } from "aurelia-fetch-client";
import { SupportedCultureInfo } from "../../model/resources/SupportedCultureInfo";
@autoinject()
export class ResourceService {
constructor(private http: HttpClient) {
}
getSupportedCultures(): Promise<SupportedCultureInfo[]> {
return this.http.fetch("resource/cultures")
.then(response => response.json())
.then<SupportedCultureInfo[]>((data: any) => {
const result = new Array<SupportedCultureInfo>();
for (let i of data) {
const info = new SupportedCultureInfo();
info.cultureCode = i.cultureCode;
info.name = i.name;
info.isCurrentCulture = i.isCurrentCulture;
result.push(info);
}
return result;
});
}
}
我必须做两件不明显的事情:
any
类型,否则gulp转换器认为它仍然被键入为Response。答案 0 :(得分:3)
我不小心点击 Enter 导致评论未完成发布。我无法编辑评论,因此根据更新后的问题更新了我的答案。
链接回调:
如果API resource/cultures
将响应返回为SupportedCultureInfo[]
而getSupportedCultures()
只需要按原样返回响应,那么就不需要第二次回调。我之前发布的答案就足够了。
我猜这两种情况中的任何一种情况都很可能需要第二次回调(或出于其他原因)
SupportedCultureInfo[]
getSupportedCultures()
如果您需要第二次回调来进一步处理回复,那么您应该在回复then<TResult>
而不是回调的后续部分时调用通用json
方法链
报告错误的原因:
在更新的代码中, gulp transpiler 将data
视为Response
类型的原因是由于正在使用非通用then(response => response.json())
返回Promise<Response>
。
而是使用会返回then<SupportedCultureInfo[]>(response => response.json())
的{{1}}或Promise<SupportedCultureInfo[]>
then<any>(response => response.json())
。
使用Promise<any>
或then<SupportedCultureInfo[]>(response => response.json())
会在第二次回调中将then<any>(response => response.json())
分别视为data
或SupportedCultureInfo[]
。
any
由于getSupportedCultures(): Promise<SupportedCultureInfo[]> {
return this.http.fetch("resource/cultures")
.then<SupportedCultureInfo[]>(response => response.json())
.then(data => {
// data will be of type SupportedCultureInfo[]
});
}
的方法签名完好无损,因此应该提供强类型then<TResult>
变量。
对data
的更改意味着我们将typescript
类型指定为return
回调的类型参数,如下所示。
then<TResult>
getSupportedCultures(): Promise<SupportedCultureInfo[]> {
return this.http.fetch("resource/cultures").then<SupportedCultureInfo[]>(response => response.json());
}
时,我遇到了同样的情况。虽然我最初的想法也归咎于npm update
,但我还是深入研究了源代码,以便为此问题做出贡献。但是,在我的追求中,我发现aurelia-fetch-client
是真正的罪魁祸首。
typescript
在回调处理返回类型的方式上有一些变化。现在,需要在interface Promise<T>
回调中将所需的return
类型作为类型参数TResult
传入。