easycodingtypescript~6 min
Implement Nullable<T>
Objective: Transform object types with homomorphic mapped types.
Make every property of T accept null. Your code is type-checked with strict mode against hidden assertions. Fix every compiler error and make the assertions pass.
- Nullable<T> maps each property type X to X | null.
- Optional and readonly modifiers are preserved.
- Arrays map their element type.
Nullable<{ a: string; b?: number }>
→ { a: string | null; b?: number | null }
TypeScript (strict)
⌘/Ctrl + Enter to type-check · Esc then Tab leaves the editor
Output
> Nothing has run yet.
Interview follow-up questions
- How would you make only some keys nullable?
- What's the difference between `T | null` and `Partial<T>`?
- How does `strictNullChecks` change the meaning of this type?