Mapped, Conditional & Template Literal Types in TypeScript
From the TypeScript cheat sheet ยท Advanced Types ยท verified Jul 2026
Mapped, Conditional & Template Literal Types
Transform and construct types programmatically
typescript
// Mapped type
type Optional<T> = { [K in keyof T]?: T[K] };
// Conditional type
type IsString<T> = T extends string ? true : false;
// Template literal type
type EventName = `on${Capitalize<"click" | "focus">}`;
// "onClick" | "onFocus"๐ก Mapped types are how Partial, Required, Readonly, and Record are built internally
โก Template literal types generate union combinations โ perfect for CSS class builders
๐ infer extracts a type variable inside conditional types โ powers ReturnType, Awaited, etc.
๐ข Key remapping with "as" + Capitalize builds getters, setters, and event handlers from shapes