Sub topic 4
Excess property checks on object literals
This chapter covers the one place where TypeScript rejects extra properties on an otherwise compatible object. It assumes you know basic TypeScript syntax (annotations, interfaces, type aliases) and the earlier rule that a value is assignable when its shape contains every required member. Both are needed for what follows.
The apparent contradiction
Structural typing says that extra properties are harmless. A value with x, y and z fits a Point that needs only x and y. Yet this code fails:
interface Point {
x: number;
y: number;
}
declare function logPoint(p: Point): void;
logPoint({ x: 1, y: 2, z: 3 });
// Error: Object literal may only specify known properties,
// and 'z' does not exist in type 'Point'.
const p3 = { x: 1, y: 2, z: 3 };
logPoint(p3); // OKThe two calls pass the same data to the same function, and only one is rejected. The second call is fine because of ordinary assignability. The first call is rejected by a separate check, the excess property check. It is a deliberate addition to structural typing, not an exception to it.
What "fresh" means
The checker marks every object literal expression as fresh at the point where it is written. A fresh literal is an expression like { x: 1, y: 2, z: 3 } that appears directly where a type is expected. When a fresh literal is compared against a target type, the checker does two things:
- It runs the normal assignability check (does the literal have every required member with a compatible type?).
- It checks that every property written in the literal exists in the target type.
Freshness is lost when the literal is stored in a variable. The type of p3 above is { x: number; y: number; z: number }, an ordinary type with no freshness flag. Passing p3 later runs only step 1.
The reasoning behind the rule is that when you write a literal directly into a typed position, you are the only one who ever holds that object. An extra property in it can never be read through the target type, so it is almost certainly a mistake: a typo, a stale name after a rename, or an option put in the wrong place. A variable might be shared with other code that does use z, so extra properties on it are legitimate.
interface Options {
color?: string;
width?: number;
}
const opts: Options = { colour: "red", width: 2 };
// Error: Object literal may only specify known properties, but 'colour'
// does not exist in type 'Options'. Did you mean to write 'color'?Optional properties make this check especially useful. Without it, a misspelled optional property would simply be ignored, and no required member would be missing to alert you.
Where the check applies
The check runs wherever a fresh literal meets a target type. These sites all report the error:
interface Point {
x: number;
y: number;
}
interface Segment {
start: Point;
end: Point;
}
// Variable declaration with an annotation
const a: Point = { x: 1, y: 2, z: 3 }; // Error
// Return statement in a function with a declared return type
function makePoint(): Point {
return { x: 1, y: 2, z: 3 }; // Error
}
// Array elements
const pts: Point[] = [{ x: 1, y: 2 }, { x: 3, y: 4, z: 5 }]; // Error on the second element
// Nested literals are checked at every level
const seg: Segment = {
start: { x: 0, y: 0 },
end: { x: 1, y: 1, z: 1 }, // Error
};The check is recursive. Inner literals are fresh too, so an extra property deep in a nested structure is reported at its own position.
When the check does not run
Because the check depends on freshness, anything that removes freshness or avoids the comparison turns it off.
interface Point {
x: number;
y: number;
}
declare function logPoint(p: Point): void;
// 1. An intermediate variable
const tmp = { x: 1, y: 2, z: 3 };
logPoint(tmp); // OK
// 2. A type assertion
logPoint({ x: 1, y: 2, z: 3 } as Point); // OK
// 3. Spread: properties that arrive through spread are not checked
const extra = { z: 3 };
const fromSpread: Point = { x: 1, y: 2, ...extra }; // OK
// 4. Explicit properties next to a spread are still checked
const stillChecked: Point = { x: 1, y: 2, ...extra, w: 4 }; // Error: 'w'Assertions are the most common deliberate escape. They silence the check without changing the data, so use them only when you know the extra property is intended. Spread is the subtle one: copying an object into a literal with ... feels like writing a literal, but the checker deliberately skips the spread members.
The satisfies operator, available since TypeScript 4.9, keeps the check on and also keeps the literal's own type:
interface Point {
x: number;
y: number;
}
const good = { x: 1, y: 2 } satisfies Point;
const bad = { x: 1, y: 2, z: 3 } satisfies Point; // Error: 'z' does not exist in type 'Point'
console.log(good.x, bad.z);Use satisfies when you want validation of a configuration object without widening it to the annotation type.
Targets that accept extra properties
Some target types make every property "known", so the check has nothing to reject.
- A string index signature, such as
{ [key: string]: number }, accepts any property name whose value fits. - The types
object,{}andunknowndo not list properties, so no literal property is flagged. Assigning{ a: 1 }toobjectis fine. - Union targets are checked against the union as a whole. A property is accepted if it exists in any member, unless the union is discriminated, in which case the check narrows to the matching member first.
interface Config {
name: string;
[extra: string]: string | number;
}
const c: Config = { name: "app", retries: 3 }; // OK: index signature
type Circle = { kind: "circle"; radius: number };
type Square = { kind: "square"; side: number };
type Shape = Circle | Square;
const s: Shape = { kind: "circle", radius: 1, side: 2 };
// Error: 'side' does not exist in type 'Circle'.
// The discriminant "kind" selects Circle, so Square's properties don't count.
type A = { a: number };
type B = { b: number };
const both: A | B = { a: 1, b: 2 }; // OK: each name exists in some memberThe last case surprises people. Without a discriminant, the union A | B accepts an object that has the properties of both members.
The decision as a flow
A gotcha with function return types
Freshness also depends on how the return type is determined. If an arrow function has an explicit return type, its returned literal is checked against it. If the function is only contextually typed by an annotation on the variable, its return type is inferred from the body first, and the inferred type is then compared. By then the literal is no longer fresh.
interface Point {
x: number;
y: number;
}
const explicit = (): Point => ({ x: 1, y: 2, z: 3 }); // Error: 'z'
const inferred: () => Point = () => ({ x: 1, y: 2, z: 3 }); // OK: no error
console.log(explicit, inferred);If you rely on the check inside callbacks, such as in map, annotate the callback's return type directly.
Common errors and how to respond
When you meet the excess property error, work through these causes in order:
- A typo or an old name. Fix the name. TypeScript often adds a "Did you mean" suggestion.
- The property belongs in the type. Add it to the interface or alias. This is correct when the type is simply incomplete.
- The data is shared and the extra property is intentional. Assign the literal to a variable first, or widen the target with an index signature, or use a deliberately wider type for the parameter.
- You are forcing the literal through. A type assertion works, but it also disables the check for typos you did not intend. Prefer fixing the type.
Summary of rules
| Situation | Excess property check |
|---|---|
| Literal written directly in a typed position | Applies |
| Literal stored in a variable, then used | Does not apply |
| Literal followed by `as T` | Does not apply |
| Properties coming from a spread | Not checked |
| Explicit properties beside a spread | Checked |
| Literal with `satisfies T` | Applies |
| Target has a string index signature | Nothing is excess |
| Union without a discriminant | Property allowed if any member has it |
Exercise
Given interface Point { x: number; y: number }, which of these lines produces an excess property error, and why? (a) const a: Point = { x: 1, y: 2, z: 3 }; (b) const t = { x: 1, y: 2, z: 3 }; const b: Point = t; (c) const c: Point = { x: 1, y: 2, ...{ z: 3 } };
Show answer
Only (a) fails. The literal is fresh and z is not in Point. In (b), t is a variable with a non-fresh type, so only normal assignability applies. In (c), properties that come from a spread are not checked, so the line compiles.
Exercise
A function takes opts: { retries?: number; timeout?: number }. A caller writes run({ retires: 3 }). Explain why this is an error, and which two separate checks it would pass or fail if retires were not misspelled but still unknown to the type.
Show answer
The literal is fresh and retires is not a known property, so the excess property check rejects it. Normal assignability alone would accept it, because all target members are optional and nothing required is missing. In the target type's case, an unknown but correctly spelled property fails the same excess check, since the check compares names, not intent. (A non-fresh variable holding { retires: 3 } would be rejected by weak type detection, covered in a later chapter.)