Answer the question
In order to leave comments, you need to log in
Why does the reducer not work without specifying the type?
I am using react-redux + typescript.
Here is the simplest redsuer I use:
type msgObjType = {
name: string
email: string
msg: string
}
type msgObjActionType = {
type: string
payload: msgObjType
}
const msgReducer = function msgReducer(state = { msgs: [] as Array<msgObjType> }, action: msgObjActionType) {
switch(action.type) {
case 'ADD_MSG': {
state = {
...state,
msgs: [ ...state.msgs, action.payload ]
};
break;
}
default:
return state;
}
return state;
}
export const addMsgAC = (msgObj: msgObjType) => {
return { type: 'ADD_MSG', payload: msgObj }
}
export default msgReducer;
as Array<msgObjType>
Type 'msgObjType[]' is not assignable to type 'never[]'.
Type 'msgObjType' is not assignable to type 'never'. TS2322
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question