Skip to main content
← TypeScript Lesson 3 / 4

If It Has the Shape, It Fits

TypeScript doesn't care what you named a type — only what fields it has. If your object has the right fields with the right types, it fits — even if it also has extra stuff. That's called structural typing (a.k.a. duck typing: if it walks like a duck and quacks like a duck, it's a duck).

The target shape

interface Point { x: number; y: number }

Build your object

Your object

const p = { x: 1, y: 1 }

Toggle fields on or off, and flip each one between number and string. Watch the verdict on the right.

Type check

assignable to Point

Shapes

your object Point needs ✓ x ✓ y extras (ok)

As long as your object contains Point's required fields, it fits — having more than Point needs is fine.

Names don't matter

interface Named { name: string }

class Dog {
  name = "Rex";   // unrelated class…
}

// …yet a Dog IS a Named — same shape, names don't matter
const n: Named = new Dog();   // ✅ ok

Named and Dog were never related — but a Dog has a name: string, so it's a Named. Same shape, interchangeable.