by paradite · 10y ago · 0 comments
function myFunction(x) {
if (x === null) {
return;
}
console.log(x + 1); // x is now a number
}
Edit: Or is this example being used to show that TypeScript now support multiple types including null in the parameter?I assumed that such feature would be available by default.
y = myFunction(1); // y = 2
y = myFunction("1"); // y = "11"
the ts version wouldn't allow you to compile myFunction("1")
The point of the example is that Typescript recently added support for non-null types, but is flexible enough to analyze where types have been further restricted by an early function return.