Is it possible to destructure an object which comes from an function call without Typescript complaining?
File 1
- React Component 1
...
let obj = null // <-- object initialization
...
useEffect(() => {
obj = functionCall(...) // <-- function call that populates the object
}, [obj])
- React component 2
const { key1, key2 } = obj // <-- object destructuring
Here I get the following complain from Typescript
- Property 'key1' does not exist on type 'null'.
- Property 'key2' does not exist on type 'null'.
How can I remove those warning?
objdeclared and then filled in by auseEffectcallback looks like a mistake. It may not be one, but it looks like one. See stackoverflow.com/questions/23667086/… and stackoverflow.com/questions/14220321/… for details. Depending on where thatobjdeclaration is, it's either going to be re-created every time the component function runs, or it's going to be shared between all instances of the component. Both seem problematic.