LLD
LLD

Reputation: 163

Cannot store multiple arrays in redux state?

I want to store multiple arrays in my redux store but following code is not working.

reducer:

export interface OrderState {
    fetching: boolean;
    orders: Order[];
    error: string;
    executions: DataModel[];  <===
}

When I change executions to be a single object then there is no error:

export interface OrderState {
    fetching: boolean;
    orders: Order[];
    error: string;
    executions: DataModel;    <===
}

Could anybody please tell me how to maintain multiple arrays in redux reducer?

Error was

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);

Initial state:

export const initialState = {
    fetching: false,
    orders: [],
    error: "",
    executions: [],
}

Upvotes: 1

Views: 86

Answers (1)

Rikin
Rikin

Reputation: 5473

In your initialState try to use orders: [] as Order[] and executions: [] as DataModel[].

Upvotes: 1

Related Questions