I'm trying to make a function which takes either a number or the string 'last' and uses corresponding element from an array.
I handle the case where input is 'last'.
I've added a simplified example. TS Playground
I'm getting an error on the return state[i] line.
I tried moving dispatch... to an if block with (typeof i === 'number') but that doesn't work either.
export function moveTo(i: number | 'last') {
return (dispatch: Function, getState: ()=>string[]) => {
const state = getState();
if (i === 'last') {
i = state.length - 1;
}
dispatch({
index: i, //here i is shown to be number on hover
effect() {
return state[i]; // index expression is not of type number (shows as number | 'last')
}
});
};
}
I want to assert after the if statement that i is number so that I don't have to use as number everywhere in the effect function.