Bill
Bill

Reputation: 5150

React TypeScript: Type 'string[]' is not assignable to type 'never[]'

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

Upvotes: 5

Views: 6695

Answers (1)

Damian Green
Damian Green

Reputation: 7495

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

const [state, setState] = useState<{ array1: string[] }>({ array1: [] });

Upvotes: 14

Related Questions