Restrict null and undefined via Non-Nullable-Types in TypeScript

InstructorMarius Schulz

Share this video with your friends

Send Tweet

This lesson introduces the --strictNullChecks compiler option and explains how non-nullable types differ from nullable types. It also illustrates how you can write safer code by being explicit about null and undefined in the type system.

Brendan Whiting
~ 6 years ago

Reminds me of Swift optionals

Harrison Ifeanyichukwu
~ 6 years ago

Hello Marius, i have this question to ask:

I have been writing my projects in es6, with data type checking and all that,

now, i have decided to use typescript, it is great, removing me the need to check if a function parameter is a callback function for instance before executing it,

I am afraid that when this gets compiled, it will misbehave when given a non function argument.

Do you encourage me to still do data type checking or leave it to the user to actually write his code in typescript as well?

Marius Schulzinstructor
~ 6 years ago

@Harrison: If you're writing a library that is potentially consumed in plain JavaScript, it makes sense to perform runtime type checks on parameters at the boundaries of your library. Internally, it's probably good enough to rely on TypeScript's static types.

I would still include a type declaration file with your library that powers autocompletion and IntelliSense within the user's IDE or editor.

Harrison Ifeanyichukwu
~ 6 years ago

Thank you very much for the advice. I think typescript is great for internal project working with several team of developers.