Chapter 1TypeScript · Theory

Structural Typing

TypeScript checks whether two types are compatible based on their properties, not their names. If an object has all the required properties, it satisfies the type.

TypeScript uses structural typing. Two types are compatible if they have the same shape — the same property names and types — regardless of what they are called or where they were defined. This is sometimes called "duck typing": if it has the right fields, it qualifies.

This is different from languages like Java or C# (which use nominal typing), where you must explicitly declare that a class implements an interface for it to be compatible. In TypeScript, a plain object literal { name: string } is assignable to a class Person { name: string } with no extra syntax.

One important detail: extra properties are fine when assigning through a variable, but not when writing an object literal directly in place. TypeScript applies "excess property checking" on fresh object literals to help catch typos in property names.

This approach suits JavaScript well. Libraries and utilities can share data as long as their shapes match, without needing a shared base class.

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