Skip to content
jshacks.dev
TypeScript

How TypeScript Type Checking Works: Structural Typing

TypeScript compares the shape of values, not their declared names. Learn how structural typing works, why extra properties error, and where it surprises.

7 min read
Summarize in AI
Flat illustration of an editor window full of code, with the TypeScript, JavaScript, Node.js and Vite logos.

Two interfaces with different names but the same fields are interchangeable in TypeScript. A class instance can be passed where an object literal was expected. An object with extra properties is accepted in one place and rejected in another. These behaviors look inconsistent until you learn the rule behind them: TypeScript checks types by their shape, not by their names. This is called structural typing, and it is the key to reading almost every compiler message. This post explains the rule, walks through examples with the exact errors, covers the excess property check that confuses people, and points out where structural typing surprises you. It uses TypeScript 6.x, and the rules are described in the type compatibility page of the handbook.

Shape over name

In languages with nominal typing, a type is identified by its name and its declaration. Two classes with identical members are still different types, and you cannot use one where the other is required. TypeScript takes the other approach. A type is compatible with another if it has at least the members the other requires, with compatible types for each. The name does not matter.

shapes.tsTypeScript
interface Point {
  x: number;
  y: number;
}

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

const c: Coordinate = { x: 1, y: 2 };
const p: Point = c;

console.log(p.x + p.y);

The assignment const p: Point = c is accepted, because Coordinate has everything Point needs. The program prints:

Text
3

The reason this design fits JavaScript is that JavaScript code is full of objects built from literals, passed around and consumed by whatever function needs a few fields. Requiring a declared relationship between types would make most existing code fail to check. Structural typing describes how the language is already used.

Missing members are errors

The check goes one way. The source must have at least the members the target requires. If a required member is missing, the compiler reports exactly which one.

missing.tsTypeScript
interface Point {
  x: number;
  y: number;
}

const partial = { x: 1 };
const p: Point = partial;
Text
missing.ts(7,7): error TS2741: Property 'y' is missing in type '{ x: number; }' but required in type 'Point'.

The message names the source type, which was inferred from the literal, the missing property and the target type. Read it as a sentence: the thing you gave lacks y, and the thing you wanted requires it. Most assignment errors read this way, and once you see the pattern you can fix them quickly by adding the member, making it optional in the target or choosing a different target.

Extra members are fine, usually

The opposite direction is permissive. A value with more members than required is compatible, because it still has everything the target needs.

extra.tsTypeScript
interface Point {
  x: number;
  y: number;
}

const p3 = { x: 1, y: 2, z: 3 };
const p: Point = p3;

console.log(p.x, p.y);

This compiles, and prints:

Text
1 2

The extra property z is still on the object at runtime, but through the variable p the type says only x and y exist. That is how you can pass a richer object to a function that needs fewer fields, which is the everyday use of the rule. It is also why a function that accepts Point works for a class that has many other members, as long as it has x and y.

The excess property check

Here is the part that seems to contradict the last section. Assign an object literal directly to a typed target, and extra properties are an error.

excess.tsTypeScript
interface Point {
  x: number;
  y: number;
}

const p: Point = { x: 1, y: 2, z: 3 };
Text
excess.ts(6,32): error TS2353: Object literal may only specify known properties, and 'z' does not exist in type 'Point'.

This is a separate check, called the excess property check, and it applies only to fresh object literals, meaning literals written directly at the point of assignment or argument passing. When you wrote p3 first and assigned the variable, the literal was no longer fresh, so the check did not apply. The rule exists because a property you write in a literal that the target does not know about is most likely a typo, such as colour in place of color, and the compiler can catch it. When the object comes from a variable, it may legitimately carry more, so the check stands down.

You can opt out of the check when you really mean it. Assign through an intermediate variable, add an index signature to the target type, or declare the extra property in the type. The most honest fix is usually the last: if the object has a z, say so in the type.

Functions and parameters

The same idea applies to function parameters, with one twist. When you pass a function where a function type is expected, the parameters are compared in a way that lets a function accept fewer parameters than the target provides. A callback (item) => ... is fine where (item, index, array) => ... is expected, because the caller can simply ignore extra arguments. This is why array.map(x => x * 2) type checks without you writing the unused parameters.

Return types work the other way. A function that returns a type with more members than required is compatible, for the same reason that extra properties are allowed on values. A function returning { x: number; y: number; z: number } can be used where () => Point is expected.

Classes are structural too

Classes follow the same rule for their public members. A class instance is compatible with an interface if it has the required shape, with no implements clause needed. The implements clause is a way to ask the compiler to check that a class meets an interface at the point of declaration, which gives a better error location, but it does not change compatibility.

Private and protected members are the exception. When a class declares a private or protected member, only instances from that same declaration are compatible with it, because the member's identity matters. That gives a limited form of nominal behavior.

privates.tsTypeScript
class Secret {
  private code = 1;
}

class Other {
  private code = 1;
}

const s: Secret = new Other();
Text
privates.ts(9,7): error TS2322: Type 'Other' is not assignable to type 'Secret'.
  Types have separate declarations of a private property 'code'.

The two classes look identical, yet the compiler treats them as different because each declares its own private member. If you ever need nominal typing for plain data, such as keeping a user id distinct from an order id, a common technique is to add a unique brand property to the type, so the shapes are no longer the same.

Where structural typing surprises you

The first surprise is that two types you consider different may be interchangeable. A UserId and an OrderId that are both aliases for string are the same type to the compiler, and passing one where the other is expected will not error. Branding fixes it when it matters.

The second is excess property checks being skipped for non-fresh values, which lets typos through when the object is built in steps. If a typo slips through, adding an explicit type annotation to the variable where you build the object brings the check back to that line.

The third is that a structural check happens at compile time against the declared types, not against runtime data. An object parsed from JSON may not have the shape you declared, and the compiler cannot know. Validate the data at the boundary.

The fourth concerns empty and wide types. A parameter typed {} accepts almost anything that is not null or undefined, because every value has at least zero required members. Reaching for {} or object as a placeholder is rarely what you want. Use unknown for values you will narrow, or a real interface for values you will read.

Reading compiler messages with this in mind

When you meet an assignability error, translate it into the shape question. What members does the target require? What does the source actually have? Is the source a fresh literal, where excess properties are errors? Is there a private member forcing a nominal match? Those four questions explain nearly every message in this family, and the compiler text usually names the member involved on its second line when the problem is nested deeper.

Share

Keep reading

Flat illustration of two options weighed side by side, with the TypeScript, Node.js, npm and JavaScript logos.
TypeScript

Type Annotations vs Type Assertions

A type annotation asks the compiler to check a value, while an assertion tells it to trust you. Learn the difference, the errors and safer alternatives.

6 min read
Flat illustration of an editor window full of code, with the TypeScript, npm and Node.js logos.
TypeScript

Type Inference in TypeScript

TypeScript works out most types for you. Learn how inference treats let and const, arrays, return values and callbacks, and when to write a type yourself.

7 min read
Flat illustration of an editor window full of code, with the TypeScript, Node.js, npm, Vite and JavaScript logos.
TypeScript

What Is TypeScript, and Why Use It?

TypeScript adds static types to JavaScript and checks them before your code runs. Learn what it is, what it catches, what it costs and when to use it.

6 min read

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.