In TypeScript all types are nullable: the compiler won’t catch common pitfalls and your type system will blow up.

The domain

As you know, TypeScript allows us to explicitly declare the type of variables and parameters defining interfaces or using the built in types (string, number, boolean…).

So you may feel confident that writing such a function:

other developers will always call it with a number.

The following calls, for example, are marked as an error by the compiler:

which makes totally sense.

The problem

But what if someone calls it with a value which is undefined or null?

I’m sure (I hope) no one will call it intentionally, but what about this case:

The last call is ok for the compiler, but with result in the worst behaviour possible: actually double(undefined) won’t fail at runtime, because for JS undefined * number = NaN.

So you’ll get a nice NaN going around your application which probably (if you are lucky) will cause a run time exception.

Bottom line

Don’t rely too much on the compiler when working with TypeScript and force your fellows to perform runtime assertions.