Skip to content

TypeScript error: inferred type of 'useMutate' cannot be named without a reference to 'swr-openapi/node_modules/type-fest' #2240

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

Open
1 task done
andreafra opened this issue Apr 2, 2025 · 0 comments
Labels
bug Something isn't working swr-openapi Relevant to swr-openapi library

Comments

@andreafra
Copy link

swr-openapi version

5.1.4

Description

Hi,

I'm using openapi-fetch + swr-openapi in a TypeScript project (standard Vite configuration), and I get the following TypeScript error in my code:

// rest of client config

export const useQuery = createQueryHook(client, 'agenda-api')
export const useImmutable = createImmutableHook(client, prefix)
export const useInfinite = createInfiniteHook(client, prefix)
// TS-ERROR:
// The inferred type of 'useMutate' cannot be named without a reference to 'swr-openapi/node_modules/type-fest'. 
// This is likely not portable. A type annotation is necessary.
export const useMutate = createMutateHook(
  client,
  prefix,
  isMatch // Or any comparision function
)

Of course, I don't have type-fest directly in my dependencies, but it gets installed transitively by swr-openapi.

What could be possible solutions to this problem?

Possible solutions

  1. Of course, annotating useMutate: (...args: any[]) => any may work, but it's not useful, otherwise I'd be using JavaScript.

  2. So far, as a workaround, I've added as a custom type a reimplementation of the value returned by createMutateHook:

export const useQuery = createQueryHook(client, 'agenda-api')
export const useImmutable = createImmutableHook(client, prefix)
export const useInfinite = createInfiniteHook(client, prefix)
export const useMutate = createMutateHook(
  client,
  prefix,
  isMatch // Or any comparision function
) as UseMutate<paths>

// This is a very stupid hack to make useMutate work.
// eslint-disable-next-line @typescript-eslint/no-empty-object-type
type UseMutate<Paths extends {}> = () => <
  Path extends PathsWithMethod<Paths, 'get'>,
  R extends TypesForGetRequest<Paths, Path>,
  Init extends R['Init']
>(
  [path, init]: [Path, PartialDeep<Init>?],
  data?: R['Data'] | Promise<R['Data']> | MutatorCallback<R['Data']>,
  opts?: boolean | MutatorOptions<R['Data']>
) => Promise<(R['Data'] | undefined)[]>

Is (2) the expected solution (doubt it), or would re-exporting type-fest from swr-openapi be a solution?

I'd gladly look more into it, but I'm afraid I currently don't have the time...

Reproduction

This is the complete file:

import type { paths } from '@lib/api/types/schema'
import { API_BASE_PATH } from '@lib/constants'

import isMatch from 'lodash-es/isMatch'
import createClient, { type Middleware } from 'openapi-fetch'
import {
  createImmutableHook,
  createInfiniteHook,
  createMutateHook,
  createQueryHook
} from 'swr-openapi'

const client = createClient<paths>({
  baseUrl: API_BASE_PATH
})

const oauthMiddleware: Middleware = {
  // ...
}

// register middleware
client.use(oauthMiddleware)

export { client }

const prefix = 'my-api'

export const useQuery = createQueryHook(client, prefix)
export const useImmutable = createImmutableHook(client, prefix)
export const useInfinite = createInfiniteHook(client, prefix)
export const useMutate: () => unknown = createMutateHook(
  client,
  prefix,
  isMatch // Or any comparision function
)

Expected result

TypeScript raises the following error during build (or in IDE):

The inferred type of 'useMutate' cannot be named without a reference to 'swr-openapi/node_modules/type-fest'.
This is likely not portable. A type annotation is necessary.

Extra

@andreafra andreafra added bug Something isn't working swr-openapi Relevant to swr-openapi library labels Apr 2, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working swr-openapi Relevant to swr-openapi library
Projects
None yet
Development

No branches or pull requests

1 participant