Im using TypeScript with Redux. This code works fine:
export type action =
| {
type: "do-other-thing";
name: string;
}
| {
type: "do-something";
index: number;
};
function reducer(state: state = initState, action: action): state {
switch (action.type) {
case "do-something":
if (action.index === 0) return state;
// More logic
case "do-other-thing":
// More logic
default:
return state;
}
However I want to move the return cases from the switch statement to be their own functions:
function doSomething(state: state, action : action): state {
if (action.index === 0) return state;
// more logic
}
function reducer(state: state = initState, action: action): state {
switch (action.type) {
case "do-something":
return doSomething(state, action);
case "do-other-thing":
// More logic
default:
return state;
}
Now the doSomething function has a TypeScript error:
Property 'index' does not exist on type 'action'. Property 'index' does not exist on type '{ type: "do-something"; width: number; isLandscape: boolean; }'.ts(2339)
I can see that TypeScript isn't aware that due to the switch statement the action object will have and index property. Is there a way to make it aware of this?