Lessons in this course (9)

2. Object types — interfaces vs type aliases

Basic★ Must knowLesson 2 of 9
typescript
interface Customer {
  readonly id: number;
  name: string;
  email?: string;                 // optional
  [key: string]: unknown;         // index signature (extra props)
}

type OrderStatus = "draft" | "submitted" | "shipped";   // only a type alias can name a union
type Order = {
  id: number;
  customer: Customer;
  status: OrderStatus;
  lines: OrderLine[];
};

interface Employee extends Customer { salary: number; }        // extend an interface
type Manager = Employee & { reports: Employee[] };              // intersect types
interfacetype
Object shapes✅✅
Unions, tuples, primitives, mapped/conditional types❌✅
Extendextends& intersection
Declaration merging (same name declared twice merges)✅❌

Common guidance: interface for public object contracts (and library augmentation), type for unions and computed types. Be consistent.

Structural typing: TypeScript compares shapes, not names — any object with the right properties is assignable, unlike C#'s nominal types.