Write a generic MakeOptional<T> mapped type without using Partial
This tests mapped type mechanics and the optionality modifier. A strong answer iterates over keyof T, adds the ? modifier, and preserves the original type via indexed access. A red flag is suggesting Object.assign or saying you would just use Partial.
WHAT THIS TESTS: This question probes whether you can operate at the type level in TypeScript. Specifically, it checks knowledge of mapped types, the keyof operator, indexed access types, and property modifiers. The interviewer wants to see that you understand how to transform an existing object type into a new shape without touching runtime code. It also reveals whether you know that type manipulation happens in the static type system, not during execution.
A GOOD ANSWER COVERS: First, declare a generic type parameter T. Second, use the mapped type syntax [K in keyof T] to iterate over every key in T. Third, append the optionality modifier ? to each property, which can be written explicitly as plus ? or simply ? because the plus prefix is implicit. Fourth, preserve the original property type by indexing back into T with T[K]. The complete signature is type MakeOptional<T> = { [K in keyof T]?: T[K] }. A strong candidate will also note that this is exactly how the built-in Partial<T> is implemented.
COMMON WRONG ANSWERS: A major red flag is providing a runtime JavaScript solution such as a function that mutates an object or uses Object.assign, because the prompt asks for a type, not a value. Another mistake is writing [K in T] instead of [K in keyof T], which fails because you must iterate over the keys, not the type itself. Some candidates forget the indexed access T[K] and leave the property values as undefined or any, which destroys type safety. Finally, simply saying I would use Partial<T> misses the point of the exercise, which is to demonstrate you can rebuild utility types from scratch.
LIKELY FOLLOW-UPS: The interviewer might ask how to make properties readonly instead of optional, which requires the readonly modifier. They could ask how to remove optionality with minus ?, or how to remap keys using the as clause introduced in TypeScript 4.1. Another common follow-up is creating a type that makes only a subset of keys optional, which combines mapped types with conditional types or generics with a second type parameter.
ONE CONCRETE EXAMPLE: Suppose you have a type User = { id: number; name: string; }. Applying MakeOptional<User> would produce { id?: number; name?: string; }. If you then write const u: MakeOptional<User> = { name: "Ada" };, the TypeScript compiler accepts it because id is now optional. This mirrors the behavior of Partial but proves you understand the underlying mechanics.
Read the original → typescriptlang.org
Get five bites like this every day.
Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.