1

From book Basarat - TypeScript Deep Dive

interface Point2D {
    x: number;
    y: number;
}
interface Point3D {
    x: number;
    y: number;
    z: number;
}
var point2D: Point2D = { x: 0, y: 10 }
var point3D: Point3D = { x: 0, y: 10, z: 20 }
function iTakePoint2D(point: Point2D) { /* do something */ }

iTakePoint2D(point2D); // exact match okay
iTakePoint2D(point3D); // extra information okay
iTakePoint2D({ x: 0 }); // Error: missing information `y`

I replace this line:

iTakePoint2D(point3D); // extra information okay

with

iTakePoint2D({ x: 0, y: 10, z: 20 }); 

and I have an Error. Why is now extra information not okay?

Ray Toal
  • 86,166
  • 18
  • 182
  • 232
  • Possible duplicate of [Why can I avoid excess property check in typescript just by passing a reference to an object to a function rather than the object in its literal form?](https://stackoverflow.com/questions/52852278/why-can-i-avoid-excess-property-check-in-typescript-just-by-passing-a-reference) – JB Nizet Jun 18 '19 at 06:19
  • https://www.typescriptlang.org/docs/handbook/interfaces.html#excess-property-checks – JB Nizet Jun 18 '19 at 06:20

1 Answers1

0

Object literals always undergo excess property checks when they are assigned to other types or they are passed as an argument to a function.

https://www.typescriptlang.org/docs/handbook/interfaces.html#excess-property-checks

  • 1
    Whilst we love links to external sources, we would love more detail in your response for it to be classed as a full answer. – I.T Delinquent Jun 18 '19 at 07:58