Step 2: Apply NonNullable to remove null and undefined
Using NonNullable<MaybeString> removes null and undefined, leaving only string.
Final Answer:
string -> Option A
Quick Check:
NonNullable removes null and undefined, leaving string [OK]
Hint: NonNullable removes null and undefined from union types [OK]
Common Mistakes:
Thinking the type still includes null or undefined
Confusing NonNullable with Partial or Required
Assuming the type becomes never
4.
Identify the error in this code snippet:
type User = { name: string | null };
type CleanUser = NonNullable<User>;
medium
A. There is no error; code is correct
B. The syntax for NonNullable is incorrect
C. NonNullable removes null from properties inside objects automatically
D. NonNullable cannot be applied to object types directly
Solution
Step 1: Understand what NonNullable does
NonNullable removes null and undefined from a type itself, not from nested properties.
Step 2: Analyze the code
The code is syntactically correct. NonNullable<User> results in the same type as User since User is not a union including null or undefined at the top level.
Final Answer:
There is no error; code is correct -> Option A
Quick Check:
Code compiles without errors [OK]
Hint: NonNullable can be applied to object types but does not clean nested properties [OK]
Thinking NonNullable cannot be applied to object types
5.
Given this type, how can you create a new type NonNullUser where the email property cannot be null or undefined using NonNullable?
type User = {
id: number;
email: string | null | undefined;
};
hard
A. type NonNullUser = NonNullable<User>
B. type NonNullUser = { id: number; email: NonNullable<string | null | undefined> }
C. type NonNullUser = { id: number; email: string }
D. type NonNullUser = { id: number; email: string | null }
Solution
Step 1: Understand the goal
We want to keep id as is, but make email exclude null and undefined.
Step 2: Apply NonNullable to the email property only
Using NonNullable<string | null | undefined> on email removes null and undefined, leaving just string.
Step 3: Check other options
type NonNullUser = NonNullable<User> removes null/undefined from the whole User type, which does not affect nested properties. type NonNullUser = { id: number; email: string } changes the type but does not use NonNullable. type NonNullUser = { id: number; email: string | null } still allows null.
Final Answer:
type NonNullUser = { id: number; email: NonNullable<string | null | undefined> } -> Option B
Quick Check:
Apply NonNullable only to the property needing it [OK]
Hint: Use NonNullable on specific properties, not whole object [OK]
Common Mistakes:
Applying NonNullable to the whole object type
Not removing null and undefined from email
Changing type without NonNullable
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.