Issue
I have a typescript code that assembles the steps of a form and it is giving me an error at build time, this is the function and it's giving me an error on the line:
setVisitedForms((seenForms) => [...seenForms, currentIndex].sort((x, y) => x - y));
const push = useCallback(
(form = nextForm) => {
if(!!currentForm) {
const currentIndex = forms.indexOf(currentForm);
if (currentIndex >= 0) {
setVisitedForms((seenForms) => [...seenForms, currentIndex].sort((x, y) => x - y));
}
history.push(`${baseurl}${form}`);
}
},
[baseurl, history, nextForm, currentForm, forms, setVisitedForms]
);
This is the error that appears on the console. Does anyone know what I'm doing wrong?
Argument of type '(seenForms: never[]) => number[]' is not assignable to parameter of type 'SetStateAction<never[]>'.
Solution
Sounds like you don't intend the type of visitedForms
to be never[]
, but you wrote:
const [visitedForms, setVisitedForms] = useState([])
which infers the type never[]
from the default value []
.
Instead, annotate useState
with the intended type:
const [visitedForms, setVisitedForms] = useState<number[]>([])
Answered By - Ben West
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.