Sub topic 5
Excess property checks on object literals
This chapter assumes you know interfaces, type aliases, and optional properties. It also assumes you know that TypeScript assignability is structural: a source type can be assigned to a target type if it has at least the properties the target requires. Excess property checking is a deliberate exception to that rule, and it applies only in one situation.
The rule and why it exists
Structural assignability allows extra properties. A value with x, y, and z has everything a Point needs, so it can be used as a Point. The excess property check adds one restriction. When you write an object literal directly where a target type is expected, TypeScript also rejects properties the target does not declare.
interface Point {
x: number;
y: number;
}
const point3d = { x: 1, y: 2, z: 3 };
const a: Point = point3d; // OK: point3d has every property Point requires
const b: Point = { x: 1, y: 2, z: 3 }; // Error: 'z' does not exist in type 'Point'The check exists because a fresh literal is a value that nothing else can refer to. If you wrote z there and the target never reads it, the property is almost certainly a typo or a leftover. A variable such as point3d may be shared with other code that does use z, so rejecting it would be wrong.
What makes a literal fresh
An object literal expression has a fresh type at the moment it is created. Freshness is lost when the literal is widened, which happens when it is stored in a variable without a type annotation, or when it is the result of a type assertion. After that, the type is a regular object type and the excess check no longer runs.
The check runs only when a fresh literal is compared against a target. These positions all qualify:
- An initializer with a type annotation:
const p: Point = { ... }. - An argument passed to a function parameter:
draw({ x: 1, y: 2, z: 3 }). - A return expression in a function with a declared return type.
- A property value inside a larger literal that is itself being checked, such as
{ origin: { x: 0, y: 0, z: 0 } }assigned to a type withorigin: Point. - Elements of an array literal assigned to an array type.
interface Point {
x: number;
y: number;
}
function draw(p: Point): void {
console.log(p.x, p.y);
}
function origin(): Point {
return { x: 0, y: 0, z: 0 }; // Error: 'z' does not exist in type 'Point'
}
draw({ x: 1, y: 2, z: 3 }); // Error: same check on an argument
const extra = { x: 1, y: 2, z: 3 };
draw(extra); // OK: extra is no longer fresh
const points: Point[] = [{ x: 1, y: 2 }, { x: 3, y: 4, z: 5 }]; // Error on 'z'The check also looks inside nested literals. It reports the first excess property it finds in a given literal, so after you fix one error another can appear.
Ways the check is skipped or satisfied
Several things stop the compiler from reporting an extra key.
- Index signatures. If the target declares
[key: string]: unknown, every key is accepted. - Union targets. For a union, a property is allowed if any member of the union declares it. For discriminated unions, TypeScript first narrows by the discriminant, so only the matching member's properties count.
- Target
object,{}, orunknown. Nothing is declared, so there is nothing to be in excess of. The check does not apply to{}andobject. - Intersections. Properties from every member of the intersection are allowed.
type Shape =
| { kind: "circle"; radius: number }
| { kind: "square"; size: number };
const s1: Shape = { kind: "circle", radius: 2 }; // OK
const s2: Shape = { kind: "circle", radius: 2, size: 4 }; // Error: 'size' does not exist in the circle member
interface Bag {
[key: string]: number;
}
const bag: Bag = { a: 1, b: 2 }; // OK: index signature accepts any keyFixing an excess property error
Choose the fix based on what you intend.
- If the property is a typo, correct it. This is the case the check is designed for.
- If the target type should have the property, add it to the type.
- If the target is meant to accept arbitrary extra keys, add an index signature.
- If you really are passing a larger object, assign it to a variable first, or restructure so the literal is not written in place.
A type assertion such as { x: 1, y: 2, z: 3 } as Point also silences the error, because assertions require only that the types be comparable. It hides real typos, so prefer the other options.
Weak types and excess properties are different checks
A common mistake is mixing up the excess property check with weak type detection. They fire in different cases.
interface Options {
color?: string;
width?: number;
}
const input = { colour: "red" };
const opts: Options = input; // Error: no properties in common with weak type 'Options'
const opts2: Options = { colour: "red" }; // Error: 'colour' does not exist; did you mean 'color'?In the first case the source is not fresh, so only weak type detection applies. In the second the source is fresh, so the excess property check reports the specific key. If the source shared at least one property, such as { color: "red", colour: "blue" } held in a variable, weak type detection would pass and the extra key would go unreported.
Common errors and edge cases
Spread does not escape the check, but it changes what is checked. Properties that come from a spread expression are not checked for excess. Only the explicitly written properties are.
interface Point {
x: number;
y: number;
}
const base = { x: 1, y: 2, z: 3 };
const p1: Point = { ...base }; // OK: spread properties are not checked
const p2: Point = { ...base, w: 4 }; // Error: 'w' does not exist in type 'Point'Callbacks returning literals. If a callback's return type is inferred, no check applies to the literal. Only a declared return type triggers it. For example, items.map((i): Point => ({ x: i, y: i, z: i })) reports z, while the same callback with no annotation does not.
Generic targets. When the target is a type parameter such as T, there is nothing declared to compare against, so no excess error is reported at the literal.
Mixed signals from optional properties. An optional property counts as declared. { color: "red", width: 1, height: 2 } fails against Options only because of height.
The check is not a runtime guard. It exists only at compile time. Data from JSON, network calls, or as casts can carry any extra keys, so validate such data explicitly if it matters.
Summary of when the check runs
| Source | Target declares the key | Result |
|---|---|---|
| Fresh literal | Yes | Allowed |
| Fresh literal | No, no index signature | Error |
| Fresh literal | No, index signature present | Allowed |
| Variable holding an object | No | Allowed, if structurally assignable |
| Fresh literal with `as Point` | No | Allowed |
| Spread properties in a literal | No | Allowed |
Exercise
Given interface User { name: string; age?: number }, which of these lines fail to compile, and why? (1) const a: User = { name: "Ana", email: "[email protected]" }; (2) const tmp = { name: "Ana", email: "[email protected]" }; const b: User = tmp; (3) const c: User = { ...tmp, nickname: "A" };
Show answer
Line 1 fails: a fresh literal with email, which User does not declare, triggers the excess property check. Line 2 compiles: tmp is not fresh, it has the required name, and extra properties are allowed under structural assignability. Line 3 fails: the spread's email is not checked, but the explicitly written nickname is not declared in User.
Exercise
You have interface Config { [key: string]: string } and write const c: Config = { host: "localhost", port: 8080 };. Does the excess property check report port? If not, which error appears?
Show answer
No excess property error appears, because the index signature accepts any key. The error is a type error on port: the number 8080 is not assignable to string, which the index signature requires for every value.