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?

It’s by design. In short, Typescript creators made it this way because they know Javascript is a very dynamic language with many such use cases.

You should read this carefully: https://www.typescriptlang.org/docs/handbook/interfaces.html#excess-property-checks
(however I bet the question arised from reading it).

Object literals get special treatment

Their logic might be like this: if you have a variable, then it may come from some third party and there is not much you can do with it. On the other hand, if you pass object literal, then you are responsible for its correct type.

Leave a Comment