Skip to content
tezvyn:
Menu

Write a generic MakeOptional<T> mapped type without using Partial

Source: typescriptlang.orgMediumHow cards are made

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's really being asked

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.

The full answer

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.

The mistakes people make

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.

What usually comes next

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.

A 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.

Interview question

Which mapped type syntax correctly makes every property of T optional while preserving the original property types?

  • a.{ [K in keyof T]?: any }
  • b.{ [K in keyof T]: T[K] | undefined }
  • c.{ [K in T]?: T[K] }
  • d.{ [K in keyof T]?: T[K] }Correct
Why?

Option D correctly iterates over keys with keyof T, applies the optional modifier with ?, and preserves each property type via indexed access T[K]. Option B is a common misconception because unioning with undefined keeps the property required rather than optional.

Just read this? Test yourself on what you have been reading.

Read the original → typescriptlang.org

Put your scrolling time to good use

Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on typescript — each one lists the topics its interview covers.

See open roles