3

以下代码使用 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>'

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

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

4

1 回答 1

17

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

它应该是:

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"
于 2021-07-29T02:04:05.397 回答