Senior (5+ years)TypeScript

What are mapped types and conditional types in TypeScript?

Quick answer

Mapped types build a new object type by looping over keys ({ [K in keyof T]: ... }). Conditional types pick a result with T extends U ? X : Y, which is how many utility types are implemented.

Partial<T> is a mapped type that adds ? to every key. You can also remap keys with as ([K in keyof T as ${K & string}Id]). Conditional types power Exclude, Extract and NonNullable. They become interesting with infer, which pulls a piece out of a type (T extends Promise<infer U> ? U : T).

Senior interviews look for judgement: these features belong in shared utility types, not in every component file. If a conditional type is hard to read, a few well-named helpers beat one clever one-liner. Recursion has a depth limit; it is the wrong tool for modelling arbitrarily nested JSON unless you must.

type ReadonlyFields<T> = { readonly [K in keyof T]: T[K] };
type Awaited<T> = T extends Promise<infer U> ? Awaited<U> : T;