redux-toolkit icon indicating copy to clipboard operation
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

Open ytftianwen opened this issue 3 years ago • 4 comments

Hi, master, i want to wrap the builder,but encountered the TS error,any thing should i do to fix this?

image

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 })
      })
    }
  }
}


ytftianwen avatar Sep 26 '22 04:09 ytftianwen

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 avatar Sep 26 '22 05:09 phryneas

@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

ytftianwen avatar Sep 26 '22 06:09 ytftianwen

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.

phryneas avatar Sep 26 '22 07:09 phryneas

The default code generation will generate the name by the url,and our API json structure is also different with openapi. so sad😭

ytftianwen avatar Sep 26 '22 09:09 ytftianwen