When you redeclare the state
property in Test
, it gets a new type based on the type of the initializer, namely {bar: undefined}
, as you can see by hovering over state
. This is why TypeScript thinks that this.state.bar
can’t be anything other than undefined
, even after you checked that it was truthy. Initializing the state
in the constructor is the correct solution.
solved Typescript generics and class properties