You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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 configexportconstuseQuery=createQueryHook(client,'agenda-api')exportconstuseImmutable=createImmutableHook(client,prefix)exportconstuseInfinite=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.exportconstuseMutate=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
Of course, annotating useMutate: (...args: any[]) => any may work, but it's not useful, otherwise I'd be using JavaScript.
So far, as a workaround, I've added as a custom type a reimplementation of the value returned by createMutateHook:
exportconstuseQuery=createQueryHook(client,'agenda-api')exportconstuseImmutable=createImmutableHook(client,prefix)exportconstuseInfinite=createInfiniteHook(client,prefix)exportconstuseMutate=createMutateHook(client,prefix,isMatch// Or any comparision function)asUseMutate<paths>// This is a very stupid hack to make useMutate work.// eslint-disable-next-line @typescript-eslint/no-empty-object-typetypeUseMutate<Pathsextends{}>=()=><PathextendsPathsWithMethod<Paths,'get'>,RextendsTypesForGetRequest<Paths,Path>,InitextendsR['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:
importtype{paths}from'@lib/api/types/schema'import{API_BASE_PATH}from'@lib/constants'importisMatchfrom'lodash-es/isMatch'importcreateClient,{typeMiddleware}from'openapi-fetch'import{createImmutableHook,createInfiniteHook,createMutateHook,createQueryHook}from'swr-openapi'constclient=createClient<paths>({baseUrl: API_BASE_PATH})constoauthMiddleware: Middleware={// ...}// register middlewareclient.use(oauthMiddleware)export{client}constprefix='my-api'exportconstuseQuery=createQueryHook(client,prefix)exportconstuseImmutable=createImmutableHook(client,prefix)exportconstuseInfinite=createInfiniteHook(client,prefix)exportconstuseMutate: ()=>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.
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:Of course, I don't have
type-fest
directly in my dependencies, but it gets installed transitively byswr-openapi
.What could be possible solutions to this problem?
Possible solutions
Of course, annotating
useMutate: (...args: any[]) => any
may work, but it's not useful, otherwise I'd be using JavaScript.So far, as a workaround, I've added as a custom type a reimplementation of the value returned by
createMutateHook
:Is (2) the expected solution (doubt it), or would re-exporting
type-fest
fromswr-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:
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
The text was updated successfully, but these errors were encountered: