TypeScript implements the Excess Property Checking mechanism for additional safety to warn the programmer about errors in object declarations when an object literal is directly assigned to a variable of a type. The mechanism arose from the fact that TypeScript's structural typing can allow extra undeclared properties, which often leads to errors in the program logic, especially when working with APIs or forms.
Excess Property Checks were introduced to improve safety in frontend development, where the structure of objects often follows a strict contract model (for example, for serialization to JSON). When an object is created as a literal and immediately passed to a function or stored in a variable of a certain type, TS performs an "excess" check — looking for extra properties that are not described in the expected type.
A programmer's mistake may go unnoticed if an object contains a typo or an extra property, and such a property will not be used properly or may remain invisible to business logic. Furthermore, Excess Property Checks may trigger unexpectedly — for example, if the object is not explicitly typed or processed using spread operators or intermediate variables.
TypeScript applies Excess Property Checks to object literals that are assigned directly to a variable or function parameter. The check looks for all properties of the object and compares them with the declared type — if extra properties are found, a compilation error will occur.
interface UserProfile { name: string; age: number; } const user: UserProfile = { name: "Sam", age: 25, email: "sam@mail.com" // Error: extra property email };
To bypass the excess check, for example, for objects with dynamic properties or partial typing, an index signature or intermediate variables are used.
interface FlexibleUser { name: string; [prop: string]: any; // Index signature allows any new properties } const user2: FlexibleUser = { name: "Sam", age: 25, email: "sam@mail.com" // Works correctly };
Key features:
If an object is created with an extra property, assigned to a variable without a type, and then the type is re-assigned, will Excess Property Checks trigger?
No, excess checks work only when the literal is assigned directly. If the object is created beforehand and then the type is specified, extra properties are not detected.
const temp = { name: "John", age: 18, foo: "bar" }; const u: UserProfile = temp; // No error, foo ignored
Do Excess Property Checks work for classes and class instances?
No, this check is not performed on classes and class instances, only for object literals.
Is it possible to globally disable excess checks in TS settings?
No, there is no separate setting to turn it off. However, an index signature can be provided for properties or the type assertion operator ('as') can be used to explicitly indicate that a check is not needed.
const special: UserProfile = { name: "Max", age: 22, hobby: "js" } as UserProfile;
A developer creates an interface for a user form, allowing all properties through [key: string]: any, to avoid errors with extra fields.
Pros: No compilation errors with dynamic data.
Cons: Any structure errors in forms or typos are not detected, making it difficult to search for bugs.
A developer sets a strict interface and uses a separate function to transform dynamic data into a strict structure, with preliminary validation.
Pros: The interface always matches the expected contract, the compiler catches typos, high maintainability.
Cons: Additional code is needed for validation and mapping dynamic data.