Last Updated: May 2, 2026
Advanced TypeScript: conditional types, template literal types, mapped types, infer keyword, discriminated unions, and declaration files.
Conditional Types
| Pattern | Type Expression |
|---|---|
| Basic conditional | T extends U ? X : Y |
| Distributive | T extends unknown ? ... : never |
| Exclude null | type NonNull<T> = T extends null | undefined ? never : T |
infer | T extends Promise<infer U> ? U : never |
Template Literal Types
`${Prefix}${string}`Match strings starting with a prefix for event names, CSS classes.
Uppercase / CapitalizeTransform literals:
Capitalize<'hello'> yields 'Hello'.Mapped Types
| Utility | Definition |
|---|---|
Partial<T> | { [K in keyof T]?: T[K] } |
Pick<T, K> | { [P in K]: T[P] } |
| Key remapping | { [K in keyof T as `get${Capitalize<K>}`]: () => T[K] } |
Discriminated Unions
kind fieldif (shape.kind === 'circle') narrows type automatically.Exhaustivenessdefault: const _: never = shape;Pro Tip: Use never type in default case of discriminated union switches for compile-time exhaustiveness checking.