Codington
0 of 6 problems solved0%

Object Types (Deep Dive)

6 exercises

This deep dive builds on basic object types with advanced patterns:

Intersections – Combine multiple types into one with all properties:

type Named = { name: string }; type Aged = { age: number }; type Person = Named & Aged; // Has both name and age

Object Unions – Model values that could be one of several shapes:

type Shape = | { kind: 'circle'; radius: number } | { kind: 'square'; size: number };

Use the in operator to narrow unions by checking for specific properties.

Index Signatures – Create dictionary-like objects:

type Dictionary = { [key: string]: number };

Excess Property Checks – TypeScript catches typos in fresh object literals:

const user: User = { name: "Alice", agee: 30 }; // ❌ Error: 'agee'

In this category, you'll:

  • Compose shapes with intersections (A & B)
  • Model alternatives with object unions
  • Narrow unions with property checks (in operator)
  • Use index signatures for flexible dictionaries
  • Understand why excess property checks prevent bugs

By the end, you'll be comfortable with advanced object type patterns and their tradeoffs.

CodingtonCodington

© 2025 Codington. Built with TypeScript, TanStack Start and a lot of console.log().