When working with an Angular app utilizing the HttpClient service, I encountered a scenario where the code worked as expected:
this.http.get(myUrl, {responseType: 'json'})
However, I needed to send the responseType as a dynamic parameter, using something like:
this.http.get(myUrl, { responseType: (condition ? 'json' : 'blob') })
or alternatively:
let options = { responseType: 'json' };
if (condition)
options = { responseType: 'blob' };
this.http.get(myUrl, options)
Unfortunately, TypeScript generated an error stating No overload matches this call
I am seeking guidance on what mistake I might be making and how I can address this issue?
Thank you!
Note: This stackoverflow discussion about Type "json" | undefined not satisfied by "json" in Angular HttpClient
was helpful in guiding me to update my code as follows:
const options: { responseType: "json" } = { responseType: "json" };
const optionsBlob: { responseType: "blob" } = { responseType: "blob" };
this.http.get(myUrl, condition ? options : optionsBlob)
Despite making these adjustments, the compiler still shows the same error mentioned earlier.