I keep encountering this strange TypeScript compile warning that just won't go away, even though I believe it shouldn't be there in the first place...
Here are the interfaces I'm working with:
interface Props {
tasks: TaskType[]
}
interface State {
completed: boolean
selectedTask?: TaskType
}
For initializing the state, I begin with undefined
, but later in my code, I assign an actual value of type TaskType
:
class TaskBlock extends React.Component<Props, State> {
state = {
completed: false,
selectedTask: undefined
}
The warning I'm encountering is within my render
method:
const {selectedTask} = this.state
...
const isSelected = selectedTask && selectedTask.id === task.id
The warning message appears at this specific point: https://i.sstatic.net/nfNID.png
Right before the selectedTask.id
statement, I am confirming that selectedTask
is NOT undefined. Despite this, TypeScript is suggesting that the object could still be 'undefined'. It's quite puzzling...
Have I overlooked something obvious here?