Skip to content

Request parameter#29

Open
ghost wants to merge 5 commits intodevelopfrom
unknown repository
Open

Request parameter#29
ghost wants to merge 5 commits intodevelopfrom
unknown repository

Conversation

@ghost
Copy link
Copy Markdown

@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.

Comment thread src/index.d.ts Outdated
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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