Search code examples
reactjstypescriptrtk-query

react ts(2742) cannot be named without a reference


i got this error ts(2742) cannot be named without a reference for all rtk query actions, how i can fix it?

import { createApi, fetchBaseQuery } from '@reduxjs/toolkit/query/react'

export const postsApi = createApi({
  reducerPath: "api",
  baseQuery: fetchBaseQuery({ baseUrl: "url" }),
  endpoints: (builder) => ({
    getChaptersList: builder.query({
      query: () => "/chapters-list/",
    }),
    getChapter: builder.query({
      query: (chapter:string) => `/chapters-list/${chapter}/`,
    }),
  }),
});


export const { 
    useGetChaptersListQuery,
    useGetChapterQuery 
} = postsApi;


The inferred type of 'useGetChapterQuery' cannot be named without a reference to '../../../node_modules/@reduxjs/toolkit/dist/query/react/buildHooks'. This is likely not portable. A type annotation is necessary.ts(2742)

enter image description here


Solution

  • It's easy to solve by changing the "moduleResolution" in 'tsconfig.json' or 'tsconfig.app.json' (depending on your project version) from 'Bundler' to 'Node'. 🙂

    Example of tsconfig.app.json