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

More TypeScript tasks

Back to the full TypeScript cheat sheet