Sub topic 3
Object Shapes and Extra Properties
Prerequisites
This chapter assumes basic TypeScript syntax, including interfaces, type aliases, object literals, and function parameters. It also assumes you know that a type annotation is checked at compile time and does not change the JavaScript object at runtime.
Structural typing and object shapes
TypeScript uses structural typing. The structure of a value means the members it has, including property names, property types, and required or optional status.
When TypeScript checks whether one object can be used where another is expected, it compares their required members. The source object is assignable to the target type when it contains compatible members for every requirement in the target.
interface Point {
x: number;
y: number;
}
function logPoint(point: Point): void {
console.log(`${point.x}, ${point.y}`);
}
const pointWithExtraData = {
x: 1,
y: 2,
label: "start"
};
logPoint(pointWithExtraData);The variable has three properties, but Point requires only x and y. The extra label property does not make the value incompatible. This is useful when a function needs only part of a larger object.
A class does not need to explicitly implement an interface to be compatible with it. A value is judged by its members, not by the names used to declare its type.
Extra properties on object literals
TypeScript performs an additional check called an excess property check when you pass a fresh object literal directly to a function or assign one directly to a type.
interface Point {
x: number;
y: number;
}
function logPoint(point: Point): void {
console.log(`${point.x}, ${point.y}`);
}
// @ts-expect-error: 'z' is not declared in Point
logPoint({ x: 1, y: 2, z: 3 });
const pointWithZ = { x: 1, y: 2, z: 3 };
logPoint(pointWithZ);The first call reports an error because the literal contains z, which is not part of Point. The second call succeeds because the object first receives a variable type. At that point, ordinary structural assignability applies.
This check helps catch misspelled property names in configuration objects. It does not mean that TypeScript forbids runtime objects from having extra properties.
Designing types with extra data
If extra properties are part of the intended design, describe them explicitly with an index signature. An index signature says that additional properties have a particular key and value type.
interface Measurements {
width: number;
height: number;
[name: string]: number;
}
const panel: Measurements = {
width: 100,
height: 50,
depth: 20
};
console.log(panel.depth);Here every string-named property must contain a number. A string value for an additional property would be rejected.
Without an index signature, do not assume that a type annotation removes properties at runtime. JavaScript values can contain more data than the static type describes. This is why Object.keys returns string[] rather than only the declared keys of an interface.
Common errors
- Treating structural typing as exact object matching.
- Assuming an interface name gives a value nominal identity.
- Expecting a variable and an inline object literal to receive identical excess property checks.
- Adding a broad index signature when it would hide misspelled property names.
- Reading an undeclared property without first narrowing or declaring it.
Exercise
Why does logPoint(pointWithZ) compile when logPoint({ x: 1, y: 2, z: 3 }) produces an excess property error?
Show answer
The inline object is a fresh literal and receives an excess property check. The variable is checked by ordinary structural assignability, so its additional z property is allowed.