gpt4 book ai didi

reactjs - 当我从 Typescript 中的 createEntityAdapter 获取选择器时出现错误

转载 作者:行者123 更新时间:2023-12-01 22:58:21 25 4
gpt4 key购买 nike

在开发一个简单的 pet 项目时,我遇到了一个错误,但不知道如何修复它。我是 Typescript 的新手,通过消息错误判断错误在 TS 中。将此代码段添加到文件“apiSlice.ts”后会出现错误消息。

export const { selectAll: selectAllPizzas } = pizzasAdapter.getSelectors(
state => selectPizzasData(state) ?? initialState
)

错误文本如下所示:

ERROR in src/features/api/apiSlice.ts:44:31
TS2345: Argument of type 'unknown' is not assignable to parameter of type '{ api: CombinedState<{ getPizzas: QueryDefinition<void, BaseQueryFn<string | FetchArgs, unknown,
FetchBaseQueryError, {}, FetchBaseQueryMeta>, never, PizzaResponseType[], "api">; }, never, "api">; }'.
42 | //
43 | export const { selectAll: selectAllPizzas } = pizzasAdapter.getSelectors(
> 44 | state => selectPizzasData(state) ?? initialState
| ^^^^^
45 | )
46 |

整个文件:(注意最后三行)

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

const pizzasAdapter = createEntityAdapter()
const initialState = pizzasAdapter.getInitialState()

interface PizzaResponseType {
id: string
name: string
image: string
price: number
currencySign: string
popularityPoint: number
type: string
description: string
spec: string
}
export const apiSlice = createApi({
reducerPath: 'api',
baseQuery: fetchBaseQuery({ baseUrl: '' }),
endpoints: builder => ({
getPizzas: builder.query<PizzaResponseType[], void>({
query: () => '/pizzas',
transformResponse: (responseData: PizzaResponseType[]) => {
pizzasAdapter.setAll(initialState, responseData)
return responseData
},
}),
}),
})

export const { useGetPizzasQuery } = apiSlice

export const selectPizzasResult = apiSlice.endpoints.getPizzas.select()

// console.log(selectPizzasResult)

export const selectPizzasData = createSelector(
selectPizzasResult,
pizzasResult => pizzasResult.data
)
//
export const { selectAll: selectAllPizzas } = pizzasAdapter.getSelectors(
state => selectPizzasData(state) ?? initialState
)

RootState 是从 store.ts 文件导入的,我在 Redux Toolkit 文档中配置了商店。store.ts 文件:

const store = configureStore({
reducer: {
pizzas:pizzaSlice,
[apiSlice.reducerPath]: apiSlice.reducer,
},
middleware: getDefaultMiddleware => getDefaultMiddleware().concat(apiSlice.middleware),
})
export type RootState = ReturnType<typeof store.getState>
export default store

为参数中的状态插入了导入的 RootState 类型:

export const { selectAll: selectAllPizzas } = pizzasAdapter.getSelectors(
(state: RootState) => selectPizzasData(state) ?? initialState
)

而且这个改变什么也没做。无论如何都会出现错误:

ERROR in src/features/api/apiSlice.ts:46:5
TS2345: Argument of type '(state: RootState) => EntityState<unknown> | PizzaResponseType[]' is not assignable to parameter of type '(state: { pizzas: EntityState<unknown>; api: CombinedState<{ getPizzas: Query
Definition<void, BaseQueryFn<string | FetchArgs, unknown, FetchBaseQueryError, {}, FetchBaseQueryMeta>, never, PizzaResponseType[], "api">; }, never, "api">; }) => EntityState<...>'.
Type 'EntityState<unknown> | PizzaResponseType[]' is not assignable to type 'EntityState<unknown>'.
Type 'PizzaResponseType[]' is missing the following properties from type 'EntityState<unknown>': ids, entities
44 |
45 | export const { selectAll: selectAllPizzas } = pizzasAdapter.getSelectors(
> 46 | (state: RootState) => selectPizzasData(state) ?? initialState
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
47 | )

如何解决?

如果您想运行宠物项目。点击此链接:https://github.com/AlexKor-5/ReactPizzaApp_Ts/tree/27028d4275fef75a70eb6b0b4b129e5bf5997e78 (但更改 apiSlice 文件的末尾,使其与 Stack Overflow 当前页面上显示的文件相对应)。(src/features/api/apiSlice.ts)

还有当我写 <EntityState<PizzaResponseType> 时它应该如何。看例子:

getPizzas: builder.query<EntityState<PizzaResponseType>, void>({
query: () => '/pizzas',
transformResponse: (responseData: PizzaResponseType[]) => {
return pizzasAdapter.setAll(pizzaInitialState,responseData)
},
}),

我收到另一个错误:

TS2322: Type '(responseData: IPizzaType[]) => EntityState<unknown>' is not assignable to type '(baseQueryReturnV
alue: unknown, meta: FetchBaseQueryMeta | undefined, arg: void) => EntityState<IPizzaType> | Promise<EntityState
<IPizzaType>>'.
Type 'EntityState<unknown>' is not assignable to type 'EntityState<IPizzaType> | Promise<EntityState<IPizzaTyp
e>>'.
Type 'EntityState<unknown>' is not assignable to type 'EntityState<IPizzaType>'.
Type 'unknown' is not assignable to type 'IPizzaType'.
28 | getPizzas: builder.query<EntityState<IPizzaType>, void>({
29 | query: () => '/pizzas',
> 30 | transformResponse: (responseData: IPizzaType[]) => {
| ^^^^^^^^^^^^^^^^^
31 | return pizzasAdapter.setAll(pizzaInitialState, responseData)
32 | },
33 | }),

又怎么了?

最佳答案

您必须为 state 提供类型。

(state: RootState) => selectPizzasData(state) ?? initialState

关于reactjs - 当我从 Typescript 中的 createEntityAdapter 获取选择器时出现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72489235/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com