Consider this example where a
is possibly null
, and is explicitly declared so. Even with strict null checks enabled, typescript does not warn me about a possible error here -
let a: string | null = "hello"
function b() {
a = null
}
b()
a.replace('2', '3')
Link to the Typescript Playground.
The same code written in flow would have immediately errored out as the replace
method is not available for a null
type.
What can I do ensure type-safety in such scenarios?