Chapter 1TypeScript · Theory

Structural Typing

TypeScript checks type compatibility by shape, not by name — if an object has the right properties, it satisfies the type.

TypeScript uses structural typing — two types are compatible if they have the same shape, regardless of how they were declared or named. This is sometimes called "duck typing": if it has the required fields, it qualifies.

This contrasts with nominal typing (C++, Java) where compatibility requires explicit declaration of a class hierarchy. In TypeScript, a plain object literal { name: string } is assignable to a class Person { name: string } and vice versa.

A consequence: extra properties are fine when assigning to a variable — but not when passing an object literal directly. TypeScript applies excess property checking on fresh literals to catch typos in property names.

Structural typing makes TypeScript particularly well-suited to JavaScript's dynamic nature: libraries, utilities, and third-party objects work together as long as their shapes align, without requiring shared base classes.

interface Point { x: number; y: number; }

class Coordinate {
  x = 0;
  y = 0;
  label = 'origin'; // extra property
}

const c = new Coordinate();
const p: Point = c; // OK — Coordinate has x and y

// Fresh literal triggers excess property checking:
const p2: Point = { x: 0, y: 0, label: 'hi' }; // Error — extra 'label'
Diagram: Structural Typing