0
0
Typescriptprogramming~10 mins

Excess property checking behavior in Typescript - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Excess property checking behavior
Define Interface
Create Object Literal
Assign Object to Interface Variable
Check Properties
Pass
Use Object
When assigning an object literal to a variable typed by an interface, TypeScript checks if the object has extra properties not in the interface and reports errors if found.
Execution Sample
Typescript
interface Person {
  name: string;
  age: number;
}

const p: Person = { name: "Alice", age: 30, city: "NY" };
This code tries to assign an object with an extra property 'city' to a variable typed as Person, triggering excess property checking.
Execution Table
StepActionCheckResultError
1Define interface Person with properties name and ageN/AInterface created
2Create object literal with name, age, and cityN/AObject created
3Assign object to variable p typed as PersonCheck if object properties match Personname and age match
4Check for extra properties in objectcity is not in PersonExtra property foundError: Object literal may only specify known properties, 'city' does not exist in type 'Person'.
💡 Assignment fails due to excess property 'city' not defined in interface Person
Variable Tracker
VariableStartAfter AssignmentFinal
pundefined{ name: 'Alice', age: 30, city: 'NY' } (error)undefined (assignment rejected)
Key Moments - 2 Insights
Why does TypeScript give an error for the extra 'city' property?
Because when assigning an object literal directly to a typed variable, TypeScript checks for any properties not declared in the interface and reports them as errors, as shown in step 4 of the execution table.
What if the object with extra properties is assigned to a variable first, then assigned to the typed variable?
Excess property checking only applies to object literals assigned directly. If assigned to a variable first, TypeScript allows extra properties, so no error occurs.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, at which step does TypeScript detect the excess property?
AStep 4
BStep 2
CStep 3
DStep 1
💡 Hint
Check the 'Check' and 'Error' columns in the execution table rows.
According to the variable tracker, what is the final value of variable 'p' after the assignment?
AAn object with name, age, and city properties
Bundefined because assignment failed
CAn object with only name and age
Dnull
💡 Hint
Look at the 'Final' column for variable 'p' in the variable tracker.
If the object literal had no extra properties, what would happen at step 4?
ATypeScript would ignore the interface
BError would still occur
CNo error, assignment passes
DThe program would crash
💡 Hint
Refer to the 'Result' column in step 4 of the execution table.
Concept Snapshot
Excess Property Checking in TypeScript:
- Happens when assigning object literals to typed variables
- Checks for properties not in the interface
- Extra properties cause compile-time errors
- Assigning via intermediate variables skips this check
- Helps catch typos and unintended properties
Full Transcript
This visual execution shows how TypeScript performs excess property checking when assigning an object literal to a variable typed by an interface. The interface Person expects 'name' and 'age'. The object literal has an extra 'city' property. At assignment, TypeScript checks and finds 'city' is not declared in Person, causing an error. The variable 'p' does not get assigned due to this error. This check only applies to direct object literals, not variables holding objects. This helps catch mistakes early.