Docs
/
TypeScript
Chapter 7

07 — Objects & Interfaces

Core Concepts

  • Object types — inline { key: Type } syntax
  • Interfaces — named object type declarations
  • Optional propertieskey?: Type
  • Readonly propertiesreadonly key: Type
  • Index signatures[key: string]: Type for dynamic keys
  • Extending interfacesinterface B extends A
  • Interface merging — declaring the same interface twice merges them
  • Excess property checking — TS catches extra properties on object literals

Best Practices

  • Use interfaces for object shapes you'll extend or implement
  • Prefer readonly for properties that shouldn't change after creation
  • Use index signatures sparingly — prefer Record<K, V> or Map
  • Mark as many properties optional or readonly as truthfully possible