Currently in the process of restructuring my NgRx reducer to incorporate the createReducer()
function instead of the conventional switch statement. However, encountering the following errors:
ERROR in src/app/store/price.reducer.ts(17,32): error TS2339: Property 'action' does not exist on type 'IUser & TypedAction<"[Websocket Service] Get User"> & { type: "[Websocket Service] Get User"; }'.
src/app/store/price.reducer.ts(18,34): error TS2339: Property 'action' does not exist on type 'IPrice & TypedAction<"[Websocket Service] New Prices"> & { type: "[Websocket Service] New Prices"; }'.
Showcasing both my createReducer()
function and the corresponding reducer that is exported below:
const userPriceReducer = createReducer(
defaultState,
on(actions.getUser, (state, {action}) => ({ ...state, user: { firstName: <string>action.firstName, lastName: <string>action.lastName, age: <number>action.age } })),
on(actions.newPrices, (state, {action}) => ({ ...state, prices: { ...state.prices, ...{[action.product_id]: <number>action.price } } }))
)
export function reducer(state: State | undefined, action: Action) {
return userPriceReducer(state, action);
}
Furthermore, presenting my defined actions below:
export const NEW_PRICES = '[Websocket Service] New Prices';
export const GET_USER = '[Websocket Service] Get User';
export const newPrices = createAction(
NEW_PRICES,
props<IPrice>()
);
export const getUser = createAction(
GET_USER,
props<IUser>()
);
In addition, IPrice
and IUser
serve as interfaces outlining the structure of the action data.
The resources I have consulted indicate that the second argument within the curly braces following state
in the createReducer()
function (where {action}
is placed) should represent the payload of the action. Nonetheless, due to my utilization of createAction()
for defining my actions, there is no explicit payload
property present within the action as previously expected—the action itself carries the data. While employing the switch statement method, I face no obstacles when it comes to accessing and saving this data. Where am I going wrong in this scenario?