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?✗ Incorrect
NonNullable removes null and undefined, leaving only string.Which values are removed by
NonNullable<T>?✗ Incorrect
NonNullable specifically removes null and undefined from the type.If a variable is typed as
NonNullable<number | null>, what values can it hold?✗ Incorrect
The
NonNullable type removes null, so only numbers remain.What is the main benefit of using
NonNullable in your code?✗ Incorrect
NonNullable helps catch errors early by disallowing null and undefined.What happens if you apply
NonNullable to a type like boolean?✗ Incorrect
Since
boolean does not include null or undefined, it remains unchanged.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.