5

When I save an array to a state and then read it back again I get the error Type 'string[]' is not assignable to type 'never[]' what is this all about?

I made a code sandbox here: https://codesandbox.io/s/affectionate-pasteur-kj5h0 where the code runs but shows the error in red underlines

3

1 Answer 1

14

If you strongly type the generic param for useState you're all good

const [state, setState] = useState<{ array1: string[] }>({ array1: [] });
Sign up to request clarification or add additional context in comments.

3 Comments

you can extract { array1: string[] } to a type to make this easier to read if you prefer. I inlined it for simplicity
Argument of type 'any[]' is not assignable to parameter of type 'SetStateAction<{ array1: string[]; }>'. still get this when I try to set a whole array as the new state: `` tempArr.push(...data.data); setStateOfMyArr(tempArr);
looks like you're trying to push an array onto an array instead of concating, but i'd ask a different question

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.