0
0
Typescriptprogramming~20 mins

NonNullable type in Typescript - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
NonNullable Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of filtering with NonNullable?
Consider the following TypeScript code using the NonNullable utility type. What will be the output when the code runs?
Typescript
type CleanString = NonNullable<string | null | undefined>;

const values: (string | null | undefined)[] = ["hello", null, "world", undefined];

const filtered: CleanString[] = values.filter((v): v is CleanString => v !== null && v !== undefined);

console.log(filtered);
A["hello", "world"]
B["hello", null, "world", undefined]
C[null, undefined]
DTypeError at runtime
Attempts:
2 left
💡 Hint
Think about what NonNullable removes from the union type and how the filter works.
🧠 Conceptual
intermediate
1:30remaining
What does NonNullable do in TypeScript?
Choose the best description of what the NonNullable utility type does.
AIt makes all properties of T required.
BIt adds null and undefined to the type T.
CIt converts T to a string type.
DIt removes null and undefined from the type T.
Attempts:
2 left
💡 Hint
Think about what values are excluded by NonNullable.
🔧 Debug
advanced
2:30remaining
Why does this code cause a type error?
Look at this TypeScript code snippet. Why does it cause a type error?
Typescript
function processValue(value: string | null | undefined) {
  const safeValue: NonNullable<string | null | undefined> = value;
  console.log(safeValue.toUpperCase());
}

processValue(null);
AThere is no error; the code runs fine.
BAssigning 'value' directly to 'safeValue' is invalid because 'value' can be null or undefined.
CCalling toUpperCase on safeValue is invalid because safeValue is not a string.
DThe function processValue cannot accept null as an argument.
Attempts:
2 left
💡 Hint
Consider the types of 'value' and 'safeValue' and what NonNullable means.
📝 Syntax
advanced
1:30remaining
Which option correctly uses NonNullable in a type alias?
Which of the following TypeScript type aliases correctly uses NonNullable to exclude null and undefined from a union type?
Atype NoNull = NonNullable<string | number | null | undefined>;
Btype NoNull = NonNullable<string | number | null | undefined) ;
Ctype NoNull = NonNullable<string | number | null | undefined;
Dtype NoNull = NonNullable<string | number | null | undefined> => string | number;
Attempts:
2 left
💡 Hint
Check for correct syntax of type alias and NonNullable usage.
🚀 Application
expert
3:00remaining
How many items are in the resulting array after applying NonNullable filter?
Given this TypeScript code, how many items will the 'cleaned' array contain after filtering?
Typescript
const mixed: (string | null | undefined | number)[] = ["a", null, 5, undefined, "b", 0];

const cleaned: NonNullable<(string | null | undefined | number)[]> = mixed.filter((item): item is NonNullable<typeof item> => item !== null && item !== undefined);

console.log(cleaned.length);
A6
B3
C4
D2
Attempts:
2 left
💡 Hint
Count how many items are not null or undefined in the array.