Syntax Cache
BlogMethodFeaturesHow It WorksBuild a Game
  1. Home
  2. TypeScript
  3. TypeScript Union & Intersection Types
TypeScript6 exercises

TypeScript Union & Intersection Types

Practice TypeScript union and intersection types including discriminated unions, literal types, and exhaustive checking.

Warm-up1 / 1

Can you write this from memory?

Declare `id` that can be string or number

TypeScript unions (|) model values that could be one of several types. Intersections (&) combine types into a single shape that has all properties. Together they're how you build exact TypeScript type definitions.

This page covers the basics. For the full reference including discriminated unions and exhaustive checking patterns, see the union & intersection cheat sheet.

Related TypeScript Topics
TypeScript Type Narrowing: Guards, Predicates & PatternsTypeScript Interface vs Type: When to Use EachTypeScript Generics: Constraints, Defaults & Patterns

What You'll Practice: TypeScript Union & Intersection Types

Define union types with the | operatorCombine types with intersection (&)Build discriminated unions with literal discriminant propertiesHandle nullable types with union and narrowingApply exhaustive switch patterns with never

TypeScript Union & Intersection Types Sample Exercises

Example 1Difficulty: 1/5

Declare `values` as an array that can contain strings or numbers

let values: (string | number)[];
Example 2Difficulty: 2/5

Create type `Direction` for movement in a 2D grid (4 cardinal directions)

type Direction = 'up' | 'down' | 'left' | 'right';
Example 3Difficulty: 2/5

Create type Employee combining Person and { employeeId: string }

type Employee = Person & { employeeId: string };

+ 3 more exercises

Quick Reference
TypeScript Union & Intersection Types Cheat Sheet →

Copy-ready syntax examples for quick lookup

Start practicing TypeScript Union & Intersection Types

Free daily exercises with spaced repetition. No credit card required.

← Back to TypeScript Syntax Practice
Syntax Cache

Build syntax muscle memory with spaced repetition.

Product

  • Pricing
  • Our Method
  • Daily Practice
  • Design Patterns
  • Interview Prep

Resources

  • Blog
  • Compare
  • Cheat Sheets
  • Vibe Coding
  • Muscle Memory

Languages

  • Python
  • JavaScript
  • TypeScript
  • Rust
  • SQL
  • GDScript

Legal

  • Terms
  • Privacy
  • Contact

© 2026 Syntax Cache

Cancel anytime in 2 clicks. Keep access until the end of your billing period.

No refunds for partial billing periods.