Conversation
| declare module 'fetch-intercept' { | ||
| export interface FetchInterceptor { | ||
| request?(url: string, config: any): Promise<any[]> | any[]; | ||
| request?(request: any, config?: any): Promise<any[]> | any[]; |
There was a problem hiding this comment.
The signature for fetch defined in lib.dom.d.ts
declare function fetch(input?: Request | string, init?: RequestInit): Promise<Response>;So the best signature for request would be
request?(input?: Request | string, init?: RequestInit): Promise<[input, init]> | Promise<[input]> | [input, init] | [input];There was a problem hiding this comment.
I agree, this would be the best signature. However, for the given TypeScript code:
this.unregisterFetchInterceptor = fetchInterceptor.register({
request: (input, init) => {
return Promise.resolve([input, init]);
}
});
TS converts the tuple into an array and I'm getting the following error:
[ts]
Type '(input: string | Request, init: RequestInit) => Promise<(string | Request | RequestInit)[]>' is not assignable to type '(input?: string | Request, init?: RequestInit) => [any, any] | Promise<[any, any]> | [any] | Promise<[any]>'.
Type 'Promise<(string | Request | RequestInit)[]>' is not assignable to type '[any, any] | Promise<[any, any]> | [any] | Promise<[any]>'.
Type 'Promise<(string | Request | RequestInit)[]>' is not assignable to type 'Promise<[any, any]>'.
Type '(string | Request | RequestInit)[]' is not assignable to type '[any, any]'.
Property '0' is missing in type '(string | Request | RequestInit)[]'.
The only solution I found is to explicitly cast the tuple to the expected type like this:
return Promise.resolve([input, init] as [Request | string, RequestInit]);
So changing the return type seems like a breaking change. Or maybe there is some other way to avoid this error?
There was a problem hiding this comment.
Which typescript version are you using?
There was a problem hiding this comment.
@mlegenhausen, I'm using version 3.1.1 in VS Code, but my gulp tasks require 2.4.2. Same error in both versions. Could we keep the return type and only change the input parameters for now?
There was a problem hiding this comment.
Sure until TS 3 is better supported by other tools. Since TS 3 the "tuple" resolution is much better.
The
fetchfunction can be called either with a string url orRequestobject. If it's aRequest, then it won't be handled properly now.