When I specify types on a function in TypeScript, I expect non-nullable behavior by default. However, even though TypeScript versions are supposed to be non-nullable by default, I am not receiving any errors when running a function with null
or undefined
.
function sayHello(name: string){
console.log(`Hello ${name}`)
}
In the example above, both sayHello(undefined)
and sayHello(null)
should result in failure.
However, here is what actually happens in TypeScript 3.8.3:
Hello null
Hello undefined
Interestingly, vsCode does not give any warnings:
https://i.sstatic.net/c4J0i.png
I wonder why TypeScript does not issue a warning when assigning a non-nullable value as null?