0

I'm cutting my teeth on Aurelia, aurelia-fetch client and Typescript. I notice in typescript when I set up the catch function

.catch(error => //do something error)

error is defined by typescript as any but when I inspect it in the debugger I can see it is type Response

Why is the type any and not Response ?

OrdinaryOrange
  • 2,420
  • 1
  • 16
  • 25

1 Answers1

0

Note that the return type of fetch method of HttpClient is Promise<Response>:

fetch(input: Request | string, init?: RequestInit): Promise<Response>;

So the catch you are referring to comes from Promise actually. The type definition of the catch comes from TypeScript (lib.d.ts) actually:

/**
 * Attaches a callback for only the rejection of the Promise.
 * @param onrejected The callback to execute when the Promise is rejected.
 * @returns A Promise for the completion of the callback.
 */
catch<TResult = never>(onrejected?: ((reason: any) => TResult | PromiseLike<TResult>) | undefined | null): Promise<T | TResult>;

And a generic definition with any as type for the reason is quite justified as it covers all cases.

Hope this helps.

Sayan Pal
  • 4,768
  • 5
  • 43
  • 82