Sub topic 1
How TypeScript Compares Shapes: Structural Typing
Prerequisites
This chapter assumes basic TypeScript annotations such as string, number, boolean, and arrays. You should also know JavaScript objects and functions, and be able to run the TypeScript compiler or use an editor that reports type errors.
The shape is what matters
TypeScript uses structural typing. Structural typing means that a value matches a type when it has the required structure, or shape, rather than when it was declared with a particular type name.
An object’s shape includes its property names, property types, and required or optional status. Consider this interface:
interface Point {
x: number;
y: number;
}
function distanceFromOrigin(point: Point): number {
return Math.sqrt(point.x ** 2 + point.y ** 2);
}
const location = { x: 3, y: 4, label: "A" };
console.log(distanceFromOrigin(location)); // 5location has an extra label property, but it still has every property required by Point. Therefore, TypeScript allows it as an argument.
The declared name of location does not matter. TypeScript checks whether its available properties can be used wherever a Point is expected.
Interfaces and aliases can describe the same shape
An interface declaration and a type alias can both describe an object shape:
interface UserInterface {
id: number;
name: string;
}
type UserAlias = {
id: number;
name: string;
};
const user: UserInterface = { id: 1, name: "Ada" };
const sameShape: UserAlias = user;The assignment works because both types require a number called id and a string called name. TypeScript does not require the value to have been created using the exact target declaration.
This compatibility also works in the other direction:
const anotherUser: UserAlias = { id: 2, name: "Grace" };
const interfaceUser: UserInterface = anotherUser;The following table summarizes the rule:
| Source value | Required target shape | Assignable? |
|---|---|---|
| `{ id: number; name: string }` | `{ id: number; name: string }` | Yes |
| `{ id: number; name: string; role: string }` | `{ id: number; name: string }` | Usually yes |
| `{ id: string; name: string }` | `{ id: number; name: string }` | No |
Excess property checks
There is an important special case for fresh object literals. When you pass an object literal directly to a function, TypeScript checks for unexpected properties:
interface Point {
x: number;
y: number;
}
function printPoint(point: Point): void {
console.log(point.x, point.y);
}
// printPoint({ x: 3, y: 4, label: "A" });
// Error: 'label' does not exist in type 'Point'.
const labeledPoint = { x: 3, y: 4, label: "A" };
printPoint(labeledPoint); // allowedThis check often catches misspelled property names. It does not mean interfaces use nominal typing. Once the object is stored in a variable, normal structural compatibility applies.
A practical mental model
When checking an assignment, ask:
- What properties does the target require?
- Does the source provide each property?
- Does each property have a compatible type?
- Are any required properties missing?
- Is a fresh object literal triggering an excess property check?
Use interfaces or aliases to document the shape, but remember that the compiler ultimately compares members.
Exercise
Will this compile? Explain why: interface Named { name: string }; const item = { name: "Book", price: 10 }; const named: Named = item;
Show answer
Yes. item contains the required name: string property. Its extra price property does not prevent assignment because item is an existing variable, not a fresh object literal.