gpt4 book ai didi

angular - Angular 8 中的错误 : Type 'string' is not assignable to type '"body"'.

转载 作者:行者123 更新时间:2023-12-02 02:39:52 26 4
gpt4 key购买 nike

ERROR in src/app/new-applicant/new-applicant.service.ts(35,65): error TS2345: Argument of type '{ reportProgress: boolean; observe: string; headers: HttpHeaders; }' is not assignable to parameter of type '{ headers?: HttpHeaders | { [header: string]: string | string[]; }; observe?: "body"; params?: HttpParams | { [param: string]: string | string[]; }; reportProgress?: boolean; responseType?: "json"; withCredentials?: boolean; }'. Types of property 'observe' are incompatible. Type 'string' is not assignable to type '"body"'.


    public generateHeaders()
{
let access=JSON.parse(sessionStorage.getItem("meta_info")).access
// access=access.access;
console.log(access)
var headers_object = new HttpHeaders().set("Authorization", "Bearer " + access);
const httpOptions = {
reportProgress:true,
observe:'events',
headers: headers_object
};
return httpOptions
}

服务
  public registerNewApplicant(data)
{
return this.http.post(environment.baseURL+"applicant/",data,this.jwtTokenGenerator.generateHeaders())
}

最佳答案

使用 as const强制 TypeScript 推断 observe 的类型作为文字类型 "events"而不是 string :

observe: 'events' as const,

HttpClient 中的每个方法都有一堆重载,期望 observe是以下之一: "body" , "events" , "response" .

默认情况下,TypeScript 将字符串文字的类型推断为 string ,除非在某些情况下,例如初始化 constreadonly领域。 generateHeaders的返回类型最终成为

{ reportProgress: boolean; observe: string; headers: HttpHeaders; }

关于 observe的信息正在 "events"丢失,因此该值与任何重载都不匹配。

关于angular - Angular 8 中的错误 : Type 'string' is not assignable to type '"body"'.,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60380981/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com