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

Updating swc from 2.1.5 to 2.2.4 minor version got type error in fetcher. #2822

Open
NicolasMontone opened this issue Oct 26, 2023 · 1 comment

Comments

@NicolasMontone
Copy link

Bug report

Description / Observed Behavior

What kind of issues did you encounter with SWR?

I have a project that used the version ^2.1.5 (using the caret for compatible with version), it looked like ->

"dependencies": {
    "axios": "^1.5.1",
    "axios-better-stacktrace": "^2.1.5",
    "swr": "^2.1.5"
  }

My code looks like ->

// utils.ts
import axios from 'axios'

export const fetcher = ([url, params]: [
  url: string,
  params?: Record<string, string>,
]) => axios.get(url, { params }).then((res) => res.data)
import useSWR, { SWRResponse } from 'swr'
import { Investment } from 'my-sdk'

import { fetcher } from './utils'

export function useItemInvestments(
  itemId: string | null,
): SWRResponse<Investment[], Error> {
  return useSWR<Investment[], Error>(
    () => itemId && [`/api/investments`, { itemId }],
    fetcher,
  )
}

the code works correctly as expected, but after adding some deps to my project the version of swr was updated too (as expected since it has the ^ prefix in the package.json to the version 2.2.4), and I started receiving the following error:
No overload matches this call. The last overload gave the following error. Type '([url, params]: [url: string, params?: Record<string, string> | undefined]) => Promise<any>' has no properties in common with type 'Partial<PublicConfiguration<Investment[], Error, BareFetcher<Investment[]>>>'

image

The question is, Is this expected?

I fixed it by pinning the dependency (while I fix the type issue with the new version)

Expected Behavior

How did you expect SWR to behave here?

I expect that updating a minor version, the API of the hook keeps the same.

Repro Steps / Code Example

Here is a [CodeSandbox](https://codesandbox.io) link the error is in app/hook.ts

@jarangutan
Copy link

Mentioning it here as well in case :-)

This started happening from release v2.2.3 from the removal of a type in #2759. Adding this back to the SWRHook interface or reverting to a version before v2.2.3 address the type error you got.

link to interface removed

// type that was removed
  <Data = any, Error = any>(
    key: Key,
    fetcher: BareFetcher<Data> | null
  ): SWRResponse<Data, Error>

Adding something ({} or undefined) after the fetcher makes the type fall onto one of the other overloads found in the SWRHook interface.

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

No branches or pull requests

2 participants