Hey I'm pretty new to typescript and I have a piece of state that I'm trying to alter in a child component based on the previous value but, I keep getting a type error when calling the setState function.
This is the function type
setPlayerCounts?: React.Dispatch<React.SetStateAction<number[]>>;
This is where I'm getting an error on the parameter of setPlayerCounts
if(setPlayerCounts && playerCount !== undefined)
setPlayerCounts(prev => prev ? [...prev, playerCount] : [playerCount]);
Error:
Argument of type '(prev: number[]) => (number | undefined)[]' is not assignable to parameter of type 'SetStateAction<number[]>'.
Type '(prev: number[]) => (number | undefined)[]' is not assignable to type '(prevState: number[]) => number[]'.\n Type '(number | undefined)[]' is not assignable to type 'number[]'.
Type 'number | undefined' is not assignable to type 'number'.
Type 'undefined' is not assignable to type 'number'.",
}]
Any help is appreciated.
playerCount?setPlayerCounts?: React.Dispatch<React.SetStateAction<number[] | undefined>>;