angular - Angular 12 HttpClient POST 到字符串无法编译
问题描述
我有一个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 on it)
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 服务器正在返回一个 plain 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; }'
推荐阅读
- sql - 在 SQL 2012 的更新语句中使用 SUM 时聚合出错
- rust - 如何保持 slice 活着以供 C++ 使用并在之后处理它?
- python - 使用 datetime python 保持日期格式
- c++ - C++ 任务流库入门
- json - 在 JSON 中解析时;将您的模型属性作为可选或强制的最佳做法是什么?
- java - 为什么使用Scala时关闭流
- node.js - 在 Fabric Node SDK 中为通道对等点动态分配背书或链码查询角色
- c++builder - C++ Builder:“禁用编译器自动依赖输出”
- database - 如何在插入数据库之前忽略保存重复记录并继续使用 vb.net 保存其他非重复记录?
- python - 为什么元组列表不可调用?当我用参数作为元组列表调用函数时,它显示错误