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

Typescript error with RequestInfo #11736

Closed
UnknownJeff opened this issue Mar 27, 2024 · 5 comments
Closed

Typescript error with RequestInfo #11736

UnknownJeff opened this issue Mar 27, 2024 · 5 comments

Comments

@UnknownJeff
Copy link

Issue Description

We are running into a type script error after upgrading to Apollo/client 3.8.9 and we are not quite sure why.

The stack-trace is as follows:

 error TS2322: Type '(uri: RequestInfo, options: RequestInit) => Promise<Response>' is not assignable to type '{ (input: RequestInfo | URL, init?: RequestInit | undefined): Promise<Response>; (input: string | Request | URL, init?: RequestInit | undefined): Promise<...>; }'.
  Types of parameters 'uri' and 'input' are incompatible.
    Type 'RequestInfo | URL' is not assignable to type 'RequestInfo'.
      Type 'URL' is not assignable to type 'RequestInfo'.

34     fetch: (uri: RequestInfo, options: RequestInit) => {

and the code snippet that produces the error is as follow:

const httpLink = createHttpLink({
    uri: foo,
    fetch: (uri: RequestInfo, options: RequestInit) => {
      options.headers = getHeaders();
      return fetch(uri, options).then((response) => {
        if (response.status >= 500) {
          return Promise.reject(response.status);
        }
        return response;
      });
    }
  });

I checked the change logs for this version and I did not notice anything obvious that changed for the types so any help is greatly appreciated.

Other possible useful version numbers:
[email protected]
[email protected]
types/[email protected]

Link to Reproduction

Not possible in this case

Reproduction Steps

Fails during build after updating package.json to 3.8.9. Nothing else as changed with the code. Downgrading back to Apollo/[email protected] results in a successful build. 3.8.9 and above causes it to fail with the error above.

@apollo/client version

3.8.9

@jerelmiller
Copy link
Member

Hey @UnknownJeff 👋

Sorry to hear you're having issues! I'm willing to bet its related to #11449 which removed dependence on the dom library in TypeScript since you can use Apollo Client in non-browser environments. The type of fetch now relies on typeof fetch which should be found in @types/node. Not sure if an update to @types/node would help or not here?

@jerelmiller
Copy link
Member

Oh wait, I see you're manually typing the fetch function yourself. You should be able to rely on type inference here, so perhaps try removing the explicit types for the arguments?

- fetch: (uri: RequestInfo, options: RequestInit) => {
+ fetch: (uri, options) => {

Doing so should then just use the types that createHttpLink provides for the fetch function instead. See if this helps!

@UnknownJeff
Copy link
Author

That along with a minor tweak worked perfectly. Thanks for the quick assist.

Copy link
Contributor

Do you have any feedback for the maintainers? Please tell us by taking a one-minute survey. Your responses will help us understand Apollo Client usage and allow us to serve you better.

Copy link
Contributor

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
For general questions, we recommend using StackOverflow or our discord server.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 28, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants