Advertisement
Intermediate TypeScript, Structural Typing & Duck Typing

What is 'excess property checking', and why does it seem to contradict pure structural typing at first glance?

Short answer

When assigning an OBJECT LITERAL directly (not through a variable) to a typed variable/parameter, TypeScript performs a STRICTER check that flags any extra properties not in the target type — this seems to violate pure structural typing ('having more properties should be fine')…

Save TypeScript mock interview
Advertisement
AModel answer
When assigning an OBJECT LITERAL directly (not through a variable) to a typed variable/parameter, TypeScript performs a STRICTER check that flags any extra properties not in the target type — this seems to violate pure structural typing ('having more properties should be fine'), but it's a deliberate, pragmatic safety net specifically for catching typos in literals, since a literal has no other legitimate purpose than matching that exact shape.
interface Point { x: number; y: number; }
function printPoint(p: Point) { console.log(p.x, p.y); }
printPoint({ x: 1, y: 2, z: 3 }); // Error: object literal has excess property 'z'

const obj = { x: 1, y: 2, z: 3 };
printPoint(obj); // OK! Assigned via a variable, so excess property checking doesn't apply
Real-world example
Catching a typo like 'colour' instead of 'color' in a configuration object literal that would otherwise silently pass structural checks.
Likely follow-ups
  • Why does excess property checking specifically NOT apply when the object comes from a variable instead of a literal?
Related topics