我最近刚刚在Aurelia Cli项目上使用Typescript在Visual Studio 2015中进行了npm update
。P>
这是以前正在编译和运行正常的代码的示例:
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结果映射的返回类,并且该方法的返回类型也是一个强烈的承诺。
有人知道最近发生了什么变化吗?这很令人沮丧。
更新:
这是我能够工作的代码:
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;
});
}
}
我必须做两件事,这些事情并不明显:
- 使用()的通用过载来指定我要使用的返回类型
- 在第二个回调中明确声明JSON参数的
any
类型,否则Gulp Transpiler认为它仍然键入响应。
更新:
我不小心点击 Enter 导致评论未完成。我无法编辑评论,因此根据更新的问题更新答案。
链接回调:如果API resource/cultures
将响应返回为SupportedCultureInfo[]
和getSupportedCultures()
只需返回响应,那么就不需要第二个回调。我之前发布的答案就足够了。
我猜想这两种情况中的任何一种很可能需要第二个回调(或其他原因)
- API返回的类型必须映射到
SupportedCultureInfo[]
- API响应需要进一步处理,然后再从
getSupportedCultures()
将其发送回去
如果您需要第二个回调才能进一步处理响应,则应在将响应读取为json
时,而不是在回调链中的后方。
报告错误的原因:在更新的代码中,原因 Gulp Transpiler 将data
视为Response
的类型是由于使用了非生成then(response => response.json())
的事实,该事实正在返回Promise<Response>
。
而是使用then<SupportedCultureInfo[]>(response => response.json())
,该CC_17将返回Promise<SupportedCultureInfo[]>
或then<any>(response => response.json())
,该CC_19将返回aurelia-fetch-client
0。
使用then<SupportedCultureInfo[]>(response => response.json())
或then<any>(response => response.json())
分别为SupportedCultureInfo[]
或any
分别为您提供data
。
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
Varaible。
解决方案
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
传递给then<TResult>
回调。