advanced

Conditional and template literal types

Compute types from other types, infer parts, distribute over unions, and model string protocols.

Conditional types choose one type or another based on assignability and can `infer` parts of a type. Template literal types build or parse string-shaped types. Together they model event names, route params, CSS tokens, and API naming conventions.

Conditional types distribute over naked type parameters in unions. Complex recursive types can slow the compiler and confuse teammates — balance precision with readability.

					type ApiRoute = `/api/${string}`;
type UnwrapPromise<T> = T extends Promise<infer U> ? U : T;
				

On senior interviews: explain distributive behavior and when type programming is worth the maintenance cost.

Common pitfalls: mixing similar APIs and forgetting edge cases during live coding. Checklist:

  • Know extends in conditional types.
  • Use infer for extracted parts.
  • Watch distribution over unions.