Course contentsShow
← TypeScript
Lesson 1512 of 1,79634. The Limits of TypeScriptFree lesson

Keep Types Simple and Composable

Favor straightforward union and intersection types over deeply nested conditional type expressions.

Keep Types Simple and Composable

What you'll learn: How to design TypeScript types that are easy to understand, maintain, and combine by favoring straightforward unions and intersections over complex conditional expressions.

The Composability Principle

When you've learned all the powerful type-level tools TypeScript offers—conditional types, infer, template literals, mapped types—it's tempting to combine them into increasingly elaborate type expressions. But there's a hidden cost: complexity compounds faster than safety improves.

The principle of keeping types simple and composable means preferring building blocks that can be easily understood in isolation and combined predictably, rather than deeply nested type logic that requires mental gymnastics to parse.

Favor Unions and Intersections

Union types (A | B) and intersection types (A & B) are TypeScript's most composable type operators. They're:

  • Readable: Anyone can understand User | Admin or Loggable & Serializable
  • Predictable: The semantics are straightforward
  • Tooling-friendly: IDEs handle them efficiently
  • Maintainable: Easy to extend with new variants

Compare this mental model: unions are "OR," intersections are "AND." That's it. No control flow analysis needed.

The Problem with Deep Conditional Nesting

When you stack conditional types several layers deep, especially with multiple infer sites and recursive patterns, you create type expressions that are extremely fragile and hard to debug. Each layer multiplies the cognitive load and potential for unexpected edge cases.

These complex conditionals often arise from trying to encode too much logic at the type level—logic that might be better expressed through runtime validation or simpler type boundaries.

A Practical Approach

Instead of one mega-conditional that handles every edge case, define clear, simple types for each case and combine them with unions or intersections. Break complex transformations into named intermediate types. Use type aliases liberally to document intent.

Think of your types like functions: small, focused, and composable beats large and monolithic.

Key Takeaway: Resist the urge to encode complex logic in deeply nested conditional types. Prefer simple unions, intersections, and well-named type aliases that combine predictably—your future self and teammates will thank you.