redux-toolkit
redux-toolkit copied to clipboard
TS Error: Return type of exported function has or is using name 'EndpointDefinitionWithQuery' from external module xx, but cannot be named
Hi, master, i want to wrap the builder,but encountered the TS error,any thing should i do to fix this?
Here is the source code
import { OmitFromUnion } from '@reduxjs/toolkit/dist/query/tsHelpers';
import { EndpointBuilder } from '@reduxjs/toolkit/dist/query/endpointDefinitions'
import { BaseQueryFn, QueryDefinition, MutationDefinition } from '@reduxjs/toolkit/query/react'
export type RequestMethod = 'GET' | 'POST' | 'PUT' | 'DELETE' | 'OPTIONS' | 'PATCH' | 'HEAD'
export function initBuilderMaker<IModels extends Record<
Exclude<keyof IModels, number | symbol>,
{
Req: Record<string, any>
Res: Record<string, any>
}
>>() {
return (builder: EndpointBuilder<BaseQueryFn, string, string>) => {
return {
query: <T extends keyof IModels, Req = IModels[T]['Req'], Res = IModels[T]['Res']>(
url: T extends `${'GET' | 'HEAD'}/${string}` ? T : never,
config?: OmitFromUnion<
QueryDefinition<Req, BaseQueryFn<Req, Res>, string, Res, string>,
'type' | 'query'
>,
) => builder.query<Res, Req>({
...(config as any),
query: payload => ({ url, params: payload }),
}),
mutation: <T extends keyof IModels, Req = IModels[T]['Req'], Res = IModels[T]['Res']>(
url: T extends `${Exclude<RequestMethod, 'GET' | 'HEAED'>}/${string}` ? T : never,
config?: OmitFromUnion<
MutationDefinition<Req, BaseQueryFn<Req, Res>, string, Res, string>,
'type' | 'query'
>,
) => builder.mutation<Res, Req>({
...(config as any),
query: payload => ({ url, params: payload })
})
}
}
}
You're pretty much in uncharted territory there, sorry. For this specific error message it could help to disable declaration emit in your tsconfig.
@phryneas I want to package the code as a NPM library. So it won’t generate .d.ts files when i disable the 'declaration' field in tsconfig
Tbh., I don't think you'll be able to get typings on this level abstracted away. Generally, I'd recommend going the code generation route or building a full api and exposing that.
The default code generation will generate the name by the url,and our API json structure is also different with openapi. so sad😭