0
0
Typescriptprogramming~5 mins

NonNullable type in Typescript - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does the NonNullable<T> type do in TypeScript?
It creates a new type by removing null and undefined from the type T. This means the resulting type cannot be null or undefined.
Click to reveal answer
beginner
How would you use NonNullable to exclude null and undefined from a type string | null | undefined?
You write NonNullable<string | null | undefined>, which results in just string.
Click to reveal answer
beginner
True or False: NonNullable<number | null | undefined> is the same as number.
True. NonNullable removes null and undefined, leaving only number.
Click to reveal answer
intermediate
Why is NonNullable useful in TypeScript?
It helps prevent errors by ensuring variables do not hold null or undefined values, which can cause runtime problems.
Click to reveal answer
beginner
What happens if you apply NonNullable to a type that does not include null or undefined?
The type stays the same because there is nothing to remove.
Click to reveal answer
What does NonNullable<string | null | undefined> evaluate to?
Astring
Bstring | null
Cnull | undefined
Dstring | undefined
Which values are removed by NonNullable<T>?
Anull and false
Bfalse and 0
Cundefined and 0
Dnull and undefined
If a variable is typed as NonNullable<number | null>, what values can it hold?
ANumbers and null
BOnly numbers
COnly null
DNumbers and undefined
What is the main benefit of using NonNullable in your code?
ATo avoid runtime errors from null or undefined values
BTo allow more null values
CTo convert types to strings
DTo add undefined to types
What happens if you apply NonNullable to a type like boolean?
AIt becomes undefined
BIt becomes never
CThe type stays as boolean
DIt becomes null
Explain what the NonNullable type does and give an example.
Think about how it cleans a type from empty values.
You got /3 concepts.
    Why is it helpful to use NonNullable in TypeScript code?
    Consider what problems null or undefined can cause.
    You got /3 concepts.