RTK 查询 createApi 结果:"Property '....' does not exist on type 'Api<BaseQueryFn>"

RTK Query createApi results in: "Property '....' does not exist on type 'Api<BaseQueryFn>"

以下代码使用 RTK 查询创建 Redux Hook:

export const specialtiesApi = createApi({
    reducerPath: 'specialtiesApi',
    baseQuery: fetchBaseQuery({ baseUrl: 'https://someplace.com/' }),
    endpoints: (builder) => ({
        getSpecialties: builder.query({
            query: (name) => `specialties`,
        }),
    }),
});

export const { useGetSpecialtiesQuery } = specialtiesApi;

最后一行代码抛出 Typescript 编译时错误:

Property 'useGetSpecialtiesQuery' does not exist on type 'Api<BaseQueryFn<string | FetchArgs, unknown, FetchBaseQueryError, {}, FetchBaseQueryMeta>, { getSpecialties: QueryDefinition<...>; }, "specialtiesApi", never, unique symbol>'

我的代码改编自 https://redux-toolkit.js.org/rtk-query/usage-with-typescript 使用 Typescript 4.3.5。

我看不出有什么问题。有什么想法吗?

确保从 @reduxjs/toolkit/query/react 模块而不是 @reduxjs/toolkit/dist/query.

导入 createApifetchBaseQuery 函数

应该是:

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

不是:

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

包版本:

"typescript": "^4.3.5",
"@reduxjs/toolkit": "^1.6.1"