We want to make sure objects have the right properties without mistakes. Excess property checks help catch extra wrong properties, while structural compatibility lets objects work if they have the needed parts.
Excess property checks vs structural compatibility in Typescript
interface Person {
name: string;
age: number;
}
const p: Person = { name: "Alice", age: 30, location: "NY" };Excess property checks happen when you assign an object literal directly to a variable or parameter expecting a specific type.
Structural compatibility means TypeScript checks if an object has at least the required properties, ignoring extra ones when not assigned directly.
location is not in Person.interface Person {
name: string;
age: number;
}
// Excess property check error
const p1: Person = { name: "Bob", age: 25, location: "LA" };Person works because of structural compatibility.interface Person {
name: string;
age: number;
}
const extra = { name: "Bob", age: 25, location: "LA" };
const p2: Person = extra; // No error herehobby causes an error.function greet(person: Person) { console.log(`Hello, ${person.name}`); } greet({ name: "Eve", age: 28, hobby: "reading" }); // Error: excess property 'hobby'
const obj = { name: "Eve", age: 28, hobby: "reading" }; greet(obj); // No error because obj is a variable
This program shows how excess property checks cause an error when assigning an object literal with extra properties directly. But assigning a variable with extra properties to the expected type works fine.
interface Car {
make: string;
year: number;
}
// Direct object literal with extra property - error
// const car1: Car = { make: "Toyota", year: 2020, color: "red" };
// Using a variable with extra property - no error
const carDetails = { make: "Toyota", year: 2020, color: "red" };
const car2: Car = carDetails;
console.log(car2.make);
console.log(car2.year);Excess property checks only happen on fresh object literals assigned directly.
Structural compatibility allows objects with extra properties to be assigned if they come from variables.
You can use type assertions or index signatures to bypass excess property checks if needed.
Excess property checks catch extra properties in direct object literals to prevent mistakes.
Structural compatibility allows objects with extra properties to be used if assigned from variables.
This helps balance safety and flexibility in TypeScript object typing.
Practice
excess property checks primarily prevent when assigning an object literal to a variable of a specific type?Solution
Step 1: Understand excess property checks context
Excess property checks happen when assigning an object literal directly to a typed variable.Step 2: Identify what excess property checks prevent
They prevent extra properties not declared in the target type from being assigned, catching typos or mistakes.Final Answer:
Assigning object literals with extra properties not defined in the target type -> Option AQuick Check:
Excess property checks = prevent extra properties [OK]
- Confusing excess property checks with missing property errors
- Thinking property order matters
- Assuming excess checks apply to variables, not literals
Solution
Step 1: Check direct object literal assignments
const obj: {name: string} = {name: 'Alice', age: 30}; assigns a literal with an extra propertyagedirectly to a typed variable, triggering excess property checks.Step 2: Compare with other options
const extra = {name: 'Bob', age: 25}; const obj: {name: string} = extra; assigns from a variable, so excess property checks do not apply. Options C and D have no extra properties.Final Answer:
const obj: {name: string} = {name: 'Alice', age: 30}; -> Option DQuick Check:
Direct literal with extra property = error [OK]
- Thinking variables cause excess property errors
- Ignoring the difference between literals and variables
- Assuming type assertions always cause errors
interface Person { name: string; }
const extra = { name: 'Eve', age: 40 };
const p: Person = extra;
console.log(p.name);Solution
Step 1: Understand assignment from variable
Assigning from variableextrabypasses excess property checks, so assignment is allowed.Step 2: Check console output
Accessingp.nameprints 'Eve' as expected.Final Answer:
Eve -> Option CQuick Check:
Variable assignment allows extra properties, prints name [OK]
- Expecting excess property error on variable assignment
- Confusing compile-time error with runtime output
- Assuming extra properties are removed
interface Car { make: string; }
const car = { make: 'Toyota', year: 2020 };
const myCar: Car = { make: 'Honda', year: 2018 };Solution
Step 1: Identify excess property check error
Direct assignment of object literal with extra propertyyearcauses error.Step 2: Fix by assigning from variable
Assigning from variablecaravoids excess property checks and fixes error.Final Answer:
Error due to extra property 'year' in direct object literal; fix by assigning from variable -> Option AQuick Check:
Direct literal with extra prop = error; use variable [OK]
- Adding extra properties to interface unnecessarily
- Ignoring excess property check rules
- Assuming no error on direct literal with extra props
interface User { username: string; }
// Option 1
const u1: User = { username: 'john', email: 'john@example.com' };
// Option 2
const extraUser = { username: 'jane', email: 'jane@example.com' };
const u2: User = extraUser;
// Option 3
const u3 = { username: 'doe', email: 'doe@example.com' } as User;
// Option 4
const u4: User = { username: 'max' };Solution
Step 1: Analyze Option 1
Direct literal with extra propertyemailcauses excess property check error.Step 2: Analyze Option 2 and 4
Option 2 assigns from variable, so no error. Option 4 matches interface exactly, no error.Step 3: Analyze Option 3
Type assertion on direct object literal with extra propertyemailstill triggers excess property checks, causing an error.Final Answer:
Only Option 2 and Option 4 -> Option BQuick Check:
Direct literal with extras errors; variable assignment and exact match okay [OK]
- Thinking type assertions bypass excess property checks on object literals
- Thinking all direct literals with extras are allowed
- Ignoring structural compatibility rules
Ready to prove it?
Pass the test and this pattern moves unknown
