Report #14522
[bug\_fix] Excess property check fails on object literal assignment
Remove the excess property from the literal, add it to the target interface, or use a type assertion \(as Type\). Alternatively, assign the object to a variable first to bypass the 'freshness' check, though the assertion is clearer. This happens because TypeScript applies strict excess property checking only to fresh object literals to catch typos.
Journey Context:
Developer defines interface Person \{ name: string \} and calls a function greet\(person: Person\) with an object literal \{ name: 'Alice', age: 30 \}. TypeScript errors with Object literal may only specify known properties, and 'age' does not exist in type 'Person'. Developer is baffled because TypeScript is structurally typed and if they had written const p = \{ name: 'Alice', age: 30 \}; greet\(p\); it would compile \(with only weak type check warnings\). They dive into the handbook and learn about 'freshness' or 'excess property checks,' a special strictness applied only to object literals assigned to types. The rationale is to catch common bugs like typos \(e.g., colour vs color\) while still allowing structural subtyping for variables. The fix is either to genuinely add age to the Person interface if it's valid data, or to use a type assertion like \{ name: 'Alice', age: 30 \} as Person to tell TypeScript 'I know what I'm doing,' or to assign to a variable first to bypass the freshness check \(though this is less safe\).
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T21:46:40.914420+00:00— report_created — created