Skip to main content

Lesson 5 of 8

Structural Typing - TypeScript's Type Compatibility

TypeScript uses structural (duck) typing, not nominal typing. A type is compatible with another if it has all the required properties - the type name doesn't matter. This is powerful but can surprise developers coming from Java or C# where the declared type name is the contract.

11 minBy Priygop Editorial TeamUpdated 2026Peer Reviewed & Verified

Structural Typing - Compatibility Rules

Structural Typing - Compatibility Rules
// ── Structural typing - shape matters, not name ───────────────
interface Point2D { x: number; y: number; }
interface Coordinate { x: number; y: number; }

// These are DIFFERENT names but SAME structure → compatible
function logPoint(p: Point2D): void {
  console.log(`(${p.x}, ${p.y})`);
}

const coord: Coordinate = { x: 10, y: 20 };
logPoint(coord);  // ✅ works! Structure matches.

// ── Excess property checking - the exception ──────────────────
// TypeScript checks EXTRA properties on OBJECT LITERALS only:
logPoint({ x: 1, y: 2, z: 3 });
// ❌ Error: Object literal may only specify known properties.
// 'z' does not exist in type 'Point2D'

const point3D = { x: 1, y: 2, z: 3 };
logPoint(point3D);  // ✅ no error - excess check only for literals

// ── Structural compatibility for functions ─────────────────────
type Logger = (message: string) => void;
type VerboseLogger = (message: string, level: "info" | "warn") => void;

// A function with FEWER params is compatible (it ignores extras)
const simpleLog: Logger = (msg) => console.log(msg);
const handler: (msg: string) => void = simpleLog;   // ✅

// ── Structural typing pitfall - accidental compatibility ───────
interface UserId    { id: number }
interface ProductId { id: number }

function deleteUser(userId: UserId): void { /* delete user */ }
const productId: ProductId = { id: 42 };
deleteUser(productId);  // ✅ TypeScript allows this!
                         // id: number matches id: number
                         // This is WRONG semantically but TS can't tell

// Solution: Branded types (covered in Module 11)
// type UserId = number & { readonly __brand: "UserId" };

// ── Widening and narrowing in assignments ─────────────────────
let narrowType: "hello" = "hello";   // literal type
let wideType: string = narrowType;   // ✅ string is wider - ok
// narrowType = wideType;            // ❌ can't assign wider to narrower

Common Mistakes

  • Assuming type name creates a contract - TypeScript doesn't care about names, only shapes. Two completely different interfaces with the same properties are mutually assignable.
  • Relying on excess property checking for safety - it only applies to object literals. Variables passed by reference bypass it. Don't use interface shapes as a security boundary.
  • Being surprised by function parameter bivariance - TypeScript has historically allowed callbacks to be called with more or fewer arguments. Understanding structural compatibility in functions prevents subtle callback type errors.

Tip

Tip

Practice Structural Typing TypeScript in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.

Diagram
Loading diagram…

TypeScript = JavaScript + static types → fewer bugs, better tooling

Practice Task

Note

Practice Task - (1) Write a working example of Structural Typing TypeScript from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.

Quick Quiz

Common Mistake

Warning

A common mistake with Structural Typing TypeScript is skipping edge case testing - empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready typescript code.

Key Takeaways

  • TypeScript uses structural (duck) typing, not nominal typing.
  • Assuming type name creates a contract - TypeScript doesn't care about names, only shapes. Two completely different interfaces with the same properties are mutually assignable.
  • Relying on excess property checking for safety - it only applies to object literals. Variables passed by reference bypass it. Don't use interface shapes as a security boundary.
  • Being surprised by function parameter bivariance - TypeScript has historically allowed callbacks to be called with more or fewer arguments. Understanding structural compatibility in functions prevents subtle callback type errors.

Hands-on Practice Tools for this Course

Test what you learn using these free in-browser developer utilities (no signup required).

All 40+ Tools

Topics in This Module

0/8 done