无法在 redux 状态下存储多个数组?
Cannot store multiple arrays in redux state?
我想在我的 redux 存储中存储多个数组,但以下代码不起作用。
减速器:
export interface OrderState {
fetching: boolean;
orders: Order[];
error: string;
executions: DataModel[]; <===
}
当我将执行更改为单个对象时,没有错误:
export interface OrderState {
fetching: boolean;
orders: Order[];
error: string;
executions: DataModel; <===
}
谁能告诉我如何在 redux reducer 中维护多个数组?
错误是
TypeScript error in src/data/store.ts(14,9)
:
Argument of type 'Partial<State> | undefined
' is not assignable to
parameter of type
{ orderbooks?: { fetching: boolean; orderbooks:
any; error: string; orderId: string; totalExecutions: string; } | {
fetching: boolean; error: any; orderbooks: never[]; orderId: string;
totalExecutions: string; } | { ...; } | { ...; } | undefined; orders?:
{ ...; } | ... 2 more ... | undefined; } | undefined'. Type
'Partial<State>' is not assignable to type '{ orderbooks?: { fetching:
boolean; orderbooks: any; error: string; orderId: string;
totalExecutions: string; } | { fetching: boolean; error: any;
orderbooks: never[]; orderId: string; totalExecutions: string; } | {
...; } | { ...; } | undefined; orders?: { ...; } | ... 2 more ... |
undefined; }
- Types of property 'orders' are incompatible.
Type 'OrderState | undefined' is not assignable to type
{ fetching: boolean; orders: any; error: string; executions: never[]; }
| { fetching: boolean; error: any; orders: never[]; executions:
never[]; } | { fetching: boolean; executions: any; error: string;
orders: never[]; } | undefined'.
Type 'OrderState' is not assignable to type '{ fetching: boolean; orders: any; error: string; executions: never[]; } | {
fetching: boolean; error: any; orders: never[]; executions: never[]; }
| { fetching: boolean; executions: any; error: string; orders:
never[]; } | undefined'.
Type 'OrderState' is not assignable to type '{ fetching: boolean; executions: any; error: string; orders: never[]; }'.
Types of property 'orders' are incompatible.
Type 'LimitOrder[]' is not assignable to type 'never[]'.
Type 'LimitOrder' is not assignable to type 'never'. TS2345
12 | const store = createStore(
13 | rootReducer,
> 14 | initialState,
| ^
15 | composeEnhancers(applyMiddleware(sagaMiddleware))
16 | );
17 | sagaMiddleware.run(rootSaga, services);
初始状态:
export const initialState = {
fetching: false,
orders: [],
error: "",
executions: [],
}
在您的 initialState 中尝试使用 orders: [] as Order[]
和 executions: [] as DataModel[]
。
我想在我的 redux 存储中存储多个数组,但以下代码不起作用。
减速器:
export interface OrderState {
fetching: boolean;
orders: Order[];
error: string;
executions: DataModel[]; <===
}
当我将执行更改为单个对象时,没有错误:
export interface OrderState {
fetching: boolean;
orders: Order[];
error: string;
executions: DataModel; <===
}
谁能告诉我如何在 redux reducer 中维护多个数组?
错误是
TypeScript error in
src/data/store.ts(14,9)
:Argument of type '
Partial<State> | undefined
' is not assignable to parameter of type{ orderbooks?: { fetching: boolean; orderbooks: any; error: string; orderId: string; totalExecutions: string; } | { fetching: boolean; error: any; orderbooks: never[]; orderId: string; totalExecutions: string; } | { ...; } | { ...; } | undefined; orders?: { ...; } | ... 2 more ... | undefined; } | undefined'. Type 'Partial<State>' is not assignable to type '{ orderbooks?: { fetching: boolean; orderbooks: any; error: string; orderId: string; totalExecutions: string; } | { fetching: boolean; error: any; orderbooks: never[]; orderId: string; totalExecutions: string; } | { ...; } | { ...; } | undefined; orders?: { ...; } | ... 2 more ... | undefined; }
- Types of property 'orders' are incompatible. Type 'OrderState | undefined' is not assignable to type
{ fetching: boolean; orders: any; error: string; executions: never[]; } | { fetching: boolean; error: any; orders: never[]; executions: never[]; } | { fetching: boolean; executions: any; error: string; orders: never[]; } | undefined'. Type 'OrderState' is not assignable to type '{ fetching: boolean; orders: any; error: string; executions: never[]; } | { fetching: boolean; error: any; orders: never[]; executions: never[]; } | { fetching: boolean; executions: any; error: string; orders: never[]; } | undefined'. Type 'OrderState' is not assignable to type '{ fetching: boolean; executions: any; error: string; orders: never[]; }'. Types of property 'orders' are incompatible. Type 'LimitOrder[]' is not assignable to type 'never[]'. Type 'LimitOrder' is not assignable to type 'never'. TS2345
12 | const store = createStore(
13 | rootReducer,
> 14 | initialState,
| ^
15 | composeEnhancers(applyMiddleware(sagaMiddleware))
16 | );
17 | sagaMiddleware.run(rootSaga, services);
初始状态:
export const initialState = {
fetching: false,
orders: [],
error: "",
executions: [],
}
在您的 initialState 中尝试使用 orders: [] as Order[]
和 executions: [] as DataModel[]
。