Solución
solution.tsTypeScript
type State = { count: number; items: string[] };
type Action = { type: string; payload?: string };
export function reducer(state: State, action: Action): State {
// Usa switch para manejar cada tipo de acción
// Retorna SIEMPRE un nuevo objeto con spread — nunca modifiques state
switch(action.type) {
case 'INCREMENT':
return { ...state, count: state.count + 1 };
case 'DECREMENT':
return { ...state, count: state.count - 1 };
case 'ADD_ITEM':
return { ...state, items: [...state.items, action.payload] };
default:
return { ...state };
}
}0respuestas