What are mapped and conditional types in TypeScript?
AdvancedTypeScript interview questionBy UIRound, updated 17 November 2026
Short answer
A mapped type builds a new object type by looping over keys: { [K in keyof T]: T[K] } copies T, and you can change each property's type, add or remove readonly and ?, or rename and filter keys with as. A conditional type, T extends U ? X : Y, picks one of two types based on assignability. With infer it can extract parts of a type, and on a union it runs once per member.
Mapped types: a loop over keys
The syntax [K in Keys] iterates over a union of keys. Combined with keyof T, it visits every property of T. This is how the built-in utility types are written.
type User = { readonly id: number; name: string; email?: string };
type MyPartial<T> = { [K in keyof T]?: T[K] };
type Flags<T> = { [K in keyof T]: boolean };
type UserFlags = Flags<User>; // { readonly id: boolean; name: boolean; email?: boolean }When the keys come from keyof T directly, the mapped type is called homomorphic. It keeps the original readonly and ? modifiers, which is why UserFlags still has a readonly id and an optional email. Homomorphic mapped types also preserve arrays and tuples: mapping over [string, number] gives a tuple back, not an object with numeric keys.
Modifiers: adding and removing readonly and ?
You can add a modifier with readonly or ? (or the explicit +readonly, +?), and remove one with a minus sign.
type Mutable<T> = { -readonly [K in keyof T]: T[K] };
type Concrete<T> = { [K in keyof T]-?: T[K] };
type EditableUser = Mutable<User>; // id is no longer readonly
type FullUser = Concrete<User>; // email is required, and its type drops undefinedRequired<T> is Concrete under another name. There is no built-in Mutable, so this is a common interview task.
Key remapping with as
Since TypeScript 4.1, an as clause after the key lets you rename keys or drop them. Mapping a key to never removes it.
type Getters<T> = {
[K in keyof T as `get${Capitalize<string & K>}`]: () => T[K];
};
type UserGetters = Getters<{ name: string; age: number }>;
// { getName: () => string; getAge: () => number }
type PickByValue<T, V> = {
[K in keyof T as T[K] extends V ? K : never]: T[K];
};
type StringFields = PickByValue<{ id: number; name: string; city: string }, string>;
// { name: string; city: string }The string & K is needed because keyof T can include number and symbol keys, and Capitalize only accepts strings. Template literal types plus remapping are how libraries generate onClick handler names from an events map, or setX setters from a state shape.
Conditional types: if/else for types
A conditional type checks whether one type is assignable to another and picks a branch.
type IsString<T> = T extends string ? true : false;
type A = IsString<"hi">; // true
type B = IsString<42>; // falseOn its own this is rarely useful. It becomes powerful with generics, infer and unions.
infer: pulling a type out of another type
Inside the extends clause, infer X declares a type variable that TypeScript fills in by matching. It is only allowed in a conditional type.
type ElementType<T> = T extends readonly (infer U)[] ? U : T;
type MyReturnType<F> = F extends (...args: any[]) => infer R ? R : never;
type Unwrap<T> = T extends Promise<infer V> ? Unwrap<V> : T;
type E = ElementType<string[]>; // string
type U = Unwrap<Promise<Promise<number>>>; // number
// infer with a constraint (TypeScript 4.7+)
type FirstString<T> = T extends [infer S extends string, ...unknown[]] ? S : never;Unwrap is recursive, which TypeScript has allowed for conditional types since 4.1. The built-in ReturnType, Parameters and Awaited are written this way.
Distribution over unions, and how to stop it
When the checked type is a bare type parameter and you pass a union, the conditional type runs once per member and joins the results. This is called a distributive conditional type.
type ToArray<T> = T extends unknown ? T[] : never;
type D = ToArray<string | number>; // string[] | number[]
type ToArrayNoDist<T> = [T] extends [unknown] ? T[] : never;
type N = ToArrayNoDist<string | number>; // (string | number)[]Distribution is what makes Exclude<T, U> = T extends U ? never : T work: each member that matches U becomes never, and never disappears from a union. Wrapping both sides in a one-element tuple, [T] extends [U], stops it because [T] is no longer a bare type parameter.
Distribution also explains a classic surprise. never is the empty union, so a distributive conditional over never runs zero times and returns never:
type IsNeverWrong<T> = T extends never ? true : false;
type W = IsNeverWrong<never>; // never, not true
type IsNever<T> = [T] extends [never] ? true : false;
type R = IsNever<never>; // trueUse distribution on purpose when you want a per-member result, for example a DistributiveOmit<T, K> = T extends unknown ? Omit<T, K> : never that keeps a discriminated union intact. The built-in Omit is not distributive.
Combining both: a recursive DeepReadonly
Real types usually mix the two features. A conditional type decides what kind of value you have; a mapped type transforms objects.
type DeepReadonly<T> = T extends (...args: any[]) => any
? T
: T extends object
? { readonly [K in keyof T]: DeepReadonly<T[K]> }
: T;Functions are returned unchanged, so methods stay callable. Arrays are objects, and because the mapped type is homomorphic, string[] becomes readonly string[], so push is a compile error.
Practise it
Write the modifier types in Implement Partial, Required and Readonly as mapped types and Implement Mutable and pick the optional and required keys. For conditional types, do Implement Exclude, Extract and NonNullable and Implement ReturnType and Parameters with infer. Then try Remap keys, IsNever, IsAny and IsUnknown and DeepReadonly and DeepPartial.
Common follow-up questions
What is the difference between a mapped type and an index signature?
An index signature, { [key: string]: number }, describes an object with any number of unknown keys. A mapped type, { [K in "a" | "b"]: number }, produces a fixed set of known keys. Record<K, V> is a mapped type, so Record<"a" | "b", number> requires both keys.
When does a conditional type not distribute?
When the checked type is not a bare type parameter: [T] extends [U], T[] extends U[], or a concrete union written inline such as string | number extends string ? .... Distribution also only happens when the generic is instantiated with a union.
Why does my conditional type stay unresolved inside a function?
If T is still a generic parameter, TypeScript cannot know which branch applies, so it defers the type. Code inside the function sees the unresolved conditional and usually needs a cast or an overload. This is why many APIs offer overloads for callers and use conditional types only in the signature.
Can mapped types change the value at runtime?
No. Like all types, they are erased. A mapped type describes the result of a transformation; you still write the JavaScript that performs it, such as an Object.fromEntries call.
Mistakes to avoid
Writing
T extends neverto test fornever; it distributes and returnsneverUse
[T] extends [never].Expecting
Omitto work per member on a discriminated unionWrite a distributive version.
Forgetting
string & Kwhen building template literal keys fromkeyof TBuilding deep recursive types for every object in an app
They slow the type checker and give hard-to-read errors; keep them for library boundaries.
Reaching for a conditional return type when two overloads or a plain generic would be simpler to read and to implement
Sources
Practise it in code
Interviewers rarely stop at the definition. These questions open in an editor with tests.
- Implement Partial, Required and Readonly as mapped typesEasy12 minFree
- Implement Exclude, Extract and NonNullable with distributive conditional typesEasy15 minFree
- Implement ReturnType and Parameters with inferEasy15 min
- Remap keys: prefix every key, build getters, and pick keys by value typeMedium30 min
- Implement IsNever, IsAny and IsUnknown without being fooled by distributionMedium25 minFree
- Implement DeepReadonly and DeepPartial for nested objects, arrays and functionsMedium30 minFree
- Implement Mutable and pick the optional and required keys of a typeEasy20 min