Skip to content
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

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from
Open

Request parameter #29

wants to merge 5 commits into from

Conversation

ghost
Copy link

@ghost ghost commented Oct 18, 2018

The fetch function can be called either with a string url or Request object. If it's a Request, then it won't be handled properly now.

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[];
Copy link
Owner

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];

Copy link
Author

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?

Copy link
Owner

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?

Copy link
Author

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?

Copy link
Owner

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants