What is excess property checking in TypeScript?

Answer

Excess property checking is a special TypeScript behavior where it raises an error when you assign an object literal directly to a typed variable if the literal has properties that the type does not declare. Example: interface Point { x: number; y: number; } const p: Point = { x: 1, y: 2, z: 3 }; — TypeScript reports an error because z is not in Point. This seems to contradict structural typing (a type with x, y, z is structurally compatible with Point which only requires x and y). The difference: excess property checking only applies to object literal assignments, not to variable-to-variable assignments: const obj = { x: 1, y: 2, z: 3 }; const p: Point = obj; — no error, because obj is not a fresh literal. This is intentional — when you write a literal directly against a type, you almost certainly made a typo if you included extra properties. Bypass excess checking by using an intermediate variable or a type assertion. Excess property checking also applies to function arguments that are object literals.