Structural Typing
Structural typing means TypeScript decides whether two types are compatible by comparing their shapes, not their names. If a value has the members a type requires, it is accepted as that type.
Detailed Explanation
TypeScript's whole type system rests on one question: is this value assignable to that type? The answer is computed from structure. A type is a description of members — property names, their types, method signatures — and any value carrying those members fits, no matter where it was declared or what it is called.
This is why you never write implements to satisfy an interface in TypeScript. An object literal, a class instance, a function return value, and a value parsed out of a config file can all satisfy the same interface without ever referring to it.
interface Point { x: number; y: number }
const anywhere = { x: 1, y: 2, label: "origin" };
const p: Point = anywhere; // fits: it has x and y
The rule for object types is the target's requirements must all be met. Extra members on the source are allowed. anywhere has a label that Point never mentioned, and that is fine — a value with more information is still usable where less was required. There is one deliberate exception: when you assign a fresh object literal directly, TypeScript runs an excess property check and rejects unknown keys, because an extra key in a literal is almost always a typo. That check is a special case bolted onto an otherwise structural system, and it is covered in the nominal vs structural entry.
Functions follow the same shape-based logic with two rules that surprise people. A function with fewer parameters is assignable to a type expecting more, because ignoring arguments is safe — that is why [1,2,3].map(n => n * 2) compiles even though map passes three arguments. And a function returning something is assignable to a type returning void, because the caller has agreed to discard the result.
Structural typing is why TypeScript can describe JavaScript that was written years before TypeScript existed. You do not need the library to cooperate. You write the shape you rely on, and any value matching it type-checks. It also makes testing pleasant: a hand-rolled object with three methods can stand in for a service interface with no mocking framework.
The costs are real. Two types that mean completely different things can be structurally identical and therefore interchangeable — a Meters and a Miles that are both number will swap freely, and the compiler has nothing to object to. Wide, popular shapes like { id: string } match far more than you intended. Error messages also get long, because a mismatch deep inside a nested structure is reported as a chain of "types of property X are incompatible" lines rather than a single name mismatch.
One escape hatch exists inside the language itself: classes with private or protected members compare by declaration, not by shape. Two classes with identical private fields are not interchangeable. That is the only place TypeScript is nominal by default.
Code Examples
1. No inheritance, no implements, no import — shape is enough.
// 1. No inheritance, no implements, no import — shape is enough.
interface Logger {
log(message: string): void;
}
class MetricsSink {
log(message: string) { /* ship to collector */ }
flush() { /* extra member, irrelevant to Logger */ }
}
function run(logger: Logger) {
logger.log("started");
}
run(new MetricsSink()); // accepted: it has log(string): void
run({ log: (m) => console.log(m) }); // accepted: m is contextually typed as string2. Function compatibility: fewer parameters is fine, extra ones are not.
// 2. Function compatibility: fewer parameters is fine, extra ones are not.
type Handler = (event: string, retries: number) => void;
const ignoresRetries: Handler = (event) => console.log(event);
// OK — a handler may ignore arguments it does not need.
const wantsMore: Handler = (event, retries, extra: boolean) => {};
// Error: Target signature provides too few arguments. Expected 3 or more, but got 2.3. Identical shapes are interchangeable, even when the meaning differs.
// 3. Identical shapes are interchangeable, even when the meaning differs.
interface Meters { value: number }
interface Miles { value: number }
function drive(distance: Miles) {}
const hike: Meters = { value: 5000 };
drive(hike); // No error. The shapes match, so the compiler has no objection.
// The one nominal corner of the language: private members compare by declaration.
class SessionToken {
private raw = "";
}
class CsrfToken {
private raw = ""; // same name, same type, different declaration
}
const token: SessionToken = new CsrfToken();
// Error: Types have separate declarations of a private property 'raw'.Common Gotchas
Matching shapes does not mean matching meaning
a user ID and an order ID that are both `string` are fully interchangeable to the compiler. If confusing them would be a bug, encode the difference with branded types instead of hoping the names protect you.
Excess properties are allowed through a variable but not through a literal
assigning `{ x, y, typo }` directly to a typed target errors, while assigning the same object via an intermediate variable does not. Both behaviours are intended; the literal check is a typo filter, not a structural rule.
A `void` return type accepts functions that return values
`() => number` is assignable to `() => void`. The result is discarded, not checked, so a callback that accidentally returns something will not be flagged.
Deep shape mismatches produce long error chains
TypeScript reports the full path through nested properties rather than a single name. Read the last line of the error first; that is the actual incompatibility.