-
Notifications
You must be signed in to change notification settings - Fork 53
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Request parameter #29
base: develop
Are you sure you want to change the base?
Conversation
src/index.d.ts
Outdated
@@ -1,7 +1,7 @@ | |||
// Typescript definition file | |||
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Which typescript version are you using?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure until TS 3 is better supported by other tools. Since TS 3 the "tuple" resolution is much better.
The
fetch
function can be called either with a string url orRequest
object. If it's aRequest
, then it won't be handled properly now.