我正在尝试使用一个(在Angular中的)方法,其定义如下:
request(method: string, url: string, options?: {
body?: any;
headers?: HttpHeaders;
params?: HttpParams;
observe?: HttpObserve;
reportProgress?: boolean;
responseType?: 'arraybuffer' | 'blob' | 'json' | 'text';
withCredentials?: boolean;
}): Observable<any>;
我的代码看起来是这样的:
let options = {
headers: headers,
content: content,
responseType: 'json'
};
http.request(method, url, options)
但是我遇到了这个错误:
由于responseType没有像"type ResponseType = 'arraybuffer' | ..."这样的声明类型,我该如何将字面量'json'“转换”为此属性的有效值?错误 TS2345:类型为“{ headers: HttpHeaders; content: string; responseType: string; }”的参数不能赋值给类型“{ body?: any; headers?: HttpHeaders; params?: HttpParams; observe?: HttpObserve; reportProgress?:...}”的参数。属性“responseType”的类型不兼容。类型“string”不能赋值给类型“"text" | "json" | "arraybuffer" | "blob"”。
'json' as const
。对于重复特别是更复杂类型的情况会有所帮助。 - p4m