简体   繁体   中英

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM