Question is about how to use the object type for sending headers, other than HttpHeaders provided in the HTTPClient declaration.
When using Angular HttpClient, I want to pass the headers with Object, I couldn't understand how to define an object of type [header: string]: string | string[]; Please help me to understand this object declaration. Same i am facing for HttpParams as well. My code approach as below.
getLoggedInUser(requestHeaderParam: GetLoggedInUserHeaderRequestParam): Observable<LoggedInUserResponse> {
return this.http.get<LoggedInUserResponse>(`${environment.apiBaseUrl}/auth/loggedInUser`,
{ headers: requestHeaderParam });
}
The error message i am getting in VS code as below
[ts] Argument of type '{ headers: GetLoggedInUserHeaderRequestParam; }' is not assignable to parameter of type '{ headers?: HttpHeaders | { [header: string]: string | string[]; }; observe?: "body"; params?: Ht...'. Types of property 'headers' are incompatible. Type 'GetLoggedInUserHeaderRequestParam' is not assignable to type 'HttpHeaders | { [header: string]: string | string[]; }'. Type 'GetLoggedInUserHeaderRequestParam' is not assignable to type '{ [header: string]: string | string[]; }'. Index signature is missing in type 'GetLoggedInUserHeaderRequestParam'.
The Request Param type as below
export interface GetLoggedInUserHeaderRequestParam {
uid: string;
PSID?: string;
}
The HttpClient Declaration as below.
HttpClient.get(url: string, options: {
headers?: HttpHeaders | {
[header: string]: string | string[];
};
observe?: "body";
params?: HttpParams | {
[param: string]: string | string[];
};
reportProgress?: boolean;
responseType: "arraybuffer";
withCredentials?: boolean;
}): Observable<ArrayBuffer>
Please help!!!
Note: My Question is not how to use HttpHeaders, I get how to use the HttpHeaders, My question is how to use the Object directly as mentioned in one of its declaration type { [header: string]: string | string[]; } in the HttpClient