Sub topic 4
Excess Property Checks on Object Literals
Prerequisites
This chapter assumes you know basic TypeScript type annotations, interfaces, type aliases, objects, and functions. It also assumes you understand structural typing: TypeScript usually compares the members a value has rather than the name of its type.
Excess property checks are an extra rule for object literals. They catch property names that do not belong to the expected object type.
The basic check
Suppose a function expects a Point:
interface Point {
x: number;
y: number;
}
function logPoint(point: Point): void {
console.log(`${point.x}, ${point.y}`);
}
logPoint({ x: 1, y: 2 }); // OK
logPoint({ x: 1, y: 2, z: 3 });
// Error: 'z' does not exist in type 'Point'The last argument has the required x and y properties, so it is structurally compatible with Point. However, it is also a fresh object literal. When a fresh object literal is checked directly against a known type, TypeScript looks for extra property names. This is an excess property check.
The check often catches spelling mistakes:
interface RequestOptions {
timeout?: number;
retries?: number;
}
const options: RequestOptions = {
tiemout: 1000
};
// Error: 'tiemout' does not exist in type 'RequestOptions'Without this extra check, a misspelled option could silently do nothing at runtime.
Why variables behave differently
Excess property checks apply to fresh object literals, not to every value that contains extra properties.
interface Point {
x: number;
y: number;
}
function logPoint(point: Point): void {
console.log(`${point.x}, ${point.y}`);
}
const pointWithExtra = { x: 1, y: 2, z: 3 };
logPoint(pointWithExtra); // OKThe variable is accepted because it has at least the members required by Point. Its z property does not prevent it from being used as a point.
This difference follows the structural type system. A function that only reads x and y can safely receive an object that also has z.
You can visualize the two paths like this:
Controlling the check
A type annotation on a variable triggers the check:
interface Point {
x: number;
y: number;
}
const checkedPoint: Point = { x: 1, y: 2, z: 3 };
// Error: 'z' does not exist in type 'Point'The satisfies operator also checks the object against a type while preserving the object's inferred properties:
interface Point {
x: number;
y: number;
}
const point = {
x: 1,
y: 2,
z: 3
} satisfies Point;
// Error: 'z' does not exist in type 'Point'If extra properties are intentional, declare them with an index signature:
interface Metadata {
name: string;
[key: string]: string;
}
const data: Metadata = {
name: "report",
owner: "Mina",
format: "csv"
};An index signature says that additional string-named properties are allowed and must have string values. Use one only when those extra properties are part of the design.
Common mistake
Do not “fix” an excess property error by adding an index signature automatically. A broad index signature can hide misspellings and weakens the information TypeScript can provide. First decide whether the property is a mistake, whether the target type needs a declared property, or whether arbitrary properties are genuinely supported.
Exercise
Why does send({ url: "/api", method: "GET", cache: true }) produce an excess property error when send expects { url: string; method: string }, but passing the same object through a variable can be accepted?
Show answer
The inline value is a fresh object literal, so TypeScript checks for unknown properties. A variable is checked structurally for the required members, and its additional cache property is allowed.