我有一个POST
方法返回一个纯字符串。
以下不编译
public ping(): Observable<string> {
return this.httpClient.post<string>(this.uri, {}, {
observe: 'body', responseType: 'text'
});
}
错误是
TS2322: Type 'Observable<ArrayBuffer>' is not assignable to type 'Observable<string>'. Type 'ArrayBuffer' is not assignable to type 'string'
看来Webstorm坚持要把我的语句编译成下面的HttpClient
(ctrl-clicked)的公共方法
post(url: string, body: any | null, options: {
headers?: HttpHeaders | {
[header: string]: string | string[];
};
context?: HttpContext;
observe?: 'body';
params?: HttpParams | {
[param: string]: string | number | boolean | ReadonlyArray<string | number | boolean>;
};
reportProgress?: boolean;
responseType: 'arraybuffer';
withCredentials?: boolean;
}): Observable<ArrayBuffer>;
我如何正确地告诉 Angular 服务器正在返回一个普通的string
?
这是通用类型令人困惑的事情,以下工作正常:
return this.httpClient.post(this.uri, {}, {
observe: 'body', responseType: 'text'
});
如果查看实现,您只能看到三个带有泛型类型参数 的重载post<T>
,所有这些都为responseType: 'json'
。鉴于编译器无法匹配任何重载,它会向您显示应用为该方法列出的第一个重载的错误,该方法确实返回Observable<ArrayBuffer>
.
请注意,对于您实际想要的重载,该observe
参数是可选的,因此您可以执行以下操作:
return this.httpClient.post(this.uri, {}, { responseType: 'text' });
此外,当我实际尝试使用损坏的服务进行构建时,我遇到了一个比在 IDE 中内联更有用的错误;它实际上告诉我没有应用任何重载:
ERROR in src/app/service.ts:11:45 - error TS2769: No overload matches this call.
Overload 1 of 15, '(url: string, body: any, options: { headers?: HttpHeaders | { [header: string]: string | string[]; }; observe: "events"; params?: HttpParams | { [param: string]: string | string[]; }; reportProgress?: boolean; responseType?: "json"; withCredentials?: boolean; }): Observable<...>', gave the following error.
Type '"text"' is not assignable to type '"json"'.
Overload 2 of 15, '(url: string, body: any, options: { headers?: HttpHeaders | { [header: string]: string | string[]; }; observe: "response"; params?: HttpParams | { [param: string]: string | string[]; }; reportProgress?: boolean; responseType?: "json"; withCredentials?: boolean; }): Observable<...>', gave the following error.
Type '"text"' is not assignable to type '"json"'.
Overload 3 of 15, '(url: string, body: any, options?: { headers?: HttpHeaders | { [header: string]: string | string[]; }; observe?: "body"; params?: HttpParams | { [param: string]: string | string[]; }; reportProgress?: boolean; responseType?: "json"; withCredentials?: boolean; }): Observable<...>', gave the following error.
Type '"text"' is not assignable to type '"json"'.
11 return this.http.post<string>("", {}, { responseType: "text" });
~~~~~~~~~~~~
node_modules/@angular/common/http/http.d.ts:2297:9
2297 responseType?: 'json';
~~~~~~~~~~~~
The expected type comes from property 'responseType' which is declared here on type '{ headers?: HttpHeaders | { [header: string]: string | string[]; }; observe: "events"; params?: HttpParams | { [param: string]: string | string[]; }; reportProgress?: boolean; responseType?: "json"; withCredentials?: boolean; }'
node_modules/@angular/common/http/http.d.ts:93m2413:9
2413 responseType?: 'json';
~~~~~~~~~~~~
The expected type comes from property 'responseType' which is declared here on type '{ headers?: HttpHeaders | { [header: string]: string | string[]; }; observe: "response"; params?: HttpParams | { [param: string]: string | string[]; }; reportProgress?: boolean; responseType?: "json"; withCredentials?: boolean; }'
node_modules/@angular/common/http/http.d.ts:2458:9
2458 responseType?: 'json';
~~~~~~~~~~~~
The expected type comes from property 'responseType' which is declared here on type '{ headers?: HttpHeaders | { [header: string]: string | string[]; }; observe?: "body"; params?: HttpParams | { [param: string]: string | string[]; }; reportProgress?: boolean; responseType?: "json"; withCredentials?: boolean; }'
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句