I am setting up ts with redux and running into quite a number of issues - mostly down to my lack of knowledge, but cannot find much online. The error I am seeing is the following:
Operator '+' cannot be applied to types 'CounterState' and '1'.
The code I have is as follows:
interface CounterState {
state: number;
}
const initialState: CounterState = {
state: 0
}
interface Action {
type: string;
}
export const counterReducer = (state = initialState, action: Action) => {
switch (action.type) {
case 'INCREMENT':
return state + 1;
case 'DECREMENT':
return state - 1;
default:
return state;
}
};
If I update to this it works but it seems like I don't have to define a type for the state. The following works
const initialState = 0;
interface Action {
type: string;
}
export const counterReducer = (state = initialState, action: Action) => {
switch (action.type) {
case 'INCREMENT':
return state + 1;
case 'DECREMENT':
return state - 1;
default:
return state;
}
};
type CounterState = number; const initialState = 0;