Sub topic 2
Assignability: Does the Shape Fit?
Prerequisites and the core rule
This chapter assumes that you know basic TypeScript syntax, including type annotations, interfaces, type aliases, and functions. You should also be comfortable with JavaScript objects and their properties.
Assignability is TypeScript's rule for deciding whether a value can be used where a particular type is expected. TypeScript mainly uses structural typing. Structural typing compares the members of a value, such as its properties and methods. It does not usually compare the names of the types that produced those members.
If the source value contains everything required by the target type, the assignment is usually valid.
Consider this interface:
interface Point {
x: number;
y: number;
}
function logPoint(point: Point): void {
console.log(`${point.x}, ${point.y}`);
}
const point3d = { x: 1, y: 2, z: 3 };
logPoint(point3d);point3d has the required x and y properties. Its additional z property does not prevent the call. The target type asks for a point with two coordinates, and a three-coordinate object can provide those coordinates.
The declared type names do not need to match:
class Vector {
constructor(
public x: number,
public y: number,
) {}
}
logPoint(new Vector(4, 5));Vector does not say that it implements Point. The call still works because its public shape contains compatible x and y properties.
Comparing shapes
When you predict assignability, inspect the target type first. Every required member of the target must exist in the source with a compatible type.
| Target requirement | Source member | Result |
|---|---|---|
| `x: number` | `x: number` | Compatible |
| `name: string` | Missing | Error |
| `active: boolean` | `active: string` | Error |
A source type may contain more members than the target. However, property types must still be compatible.
interface Named {
name: string;
}
const user = {
name: "Mina",
age: 32,
};
const namedUser: Named = user; // OKThe variable namedUser is viewed through the narrower Named type. The age property still exists at runtime, but it is not part of the type known through namedUser.
Assignability is not object validation
TypeScript checks types during compilation. It does not remove properties or validate JavaScript values at runtime. If data comes from a network request, a file, or another untyped source, you need runtime validation before trusting its shape.
A common mistake is to read the rule as “extra properties are always allowed.” TypeScript has a special check for a fresh object literal written directly where a type is expected:
interface Point {
x: number;
y: number;
}
function draw(point: Point): void {
console.log(point.x, point.y);
}
draw({ x: 1, y: 2, z: 3 });
// Error: 'z' does not exist in type 'Point'This check catches likely spelling mistakes. Moving the same object into a variable changes the situation:
const point3d = { x: 1, y: 2, z: 3 };
draw(point3d); // OKThe variable has already been inferred as an object with x, y, and z. Since it contains the required Point members, it is assignable.
Exercise
An interface requires id: number. Is { id: 7, label: "item" } assignable through a variable, and is it accepted when written directly as an argument?
Show answer
Through a variable it is assignable because it contains the required member. As a fresh object literal, it may produce an excess property error for label.