Excess property checks happen when you assign an object literal to a variable or parameter with a specific type, and TypeScript warns if the object has extra properties not defined in that type.
Click to reveal answer
beginner
Explain structural compatibility in TypeScript.
Structural compatibility means TypeScript compares the shape (properties and types) of two types to decide if one can be assigned to the other, ignoring the names of types.
Click to reveal answer
intermediate
Why does TypeScript allow extra properties when assigning variables but not with object literals?
TypeScript performs excess property checks only on object literals to catch typos or mistakes. When assigning variables, it trusts the variable's type and uses structural compatibility, allowing extra properties.
Click to reveal answer
intermediate
How can you bypass excess property checks when passing object literals?
You can assign the object literal to a variable first, or use a type assertion (like 'as') to tell TypeScript to trust the object shape, bypassing excess property checks.
Click to reveal answer
intermediate
Give an example where excess property checks cause an error but structural compatibility allows assignment.
Example: Passing {a: 1, b: 2, c: 3} directly to a function expecting {a: number, b: number} causes an error due to excess property 'c'. But assigning it to a variable first and then passing it works because of structural compatibility.
Click to reveal answer
What triggers excess property checks in TypeScript?
AAssigning a variable to another variable
BUsing type assertions
CAssigning an object literal directly to a typed variable or parameter
DDeclaring interfaces
✗ Incorrect
Excess property checks happen only when assigning object literals directly to typed variables or parameters.
What does structural compatibility in TypeScript compare?
AThe shape (properties and types) of types
BThe runtime values
CThe order of properties
DThe names of types
✗ Incorrect
Structural compatibility compares the shape, meaning the properties and their types, ignoring names.
Which of these will cause an excess property check error?
AUsing a type assertion
BAssigning {x: 1, y: 2, z: 3} to a variable typed {x: number, y: number}
CAssigning a variable with extra properties to a typed variable
DPassing {x: 1, y: 2, z: 3} directly to a function expecting {x: number, y: number}
✗ Incorrect
Assigning an object literal with extra properties directly to a typed variable or parameter triggers excess property checks and causes an error.
How can you avoid excess property check errors when passing extra properties?
ARemove all extra properties
BAssign the object literal to a variable first
CUse a different language
DIgnore TypeScript errors
✗ Incorrect
Assigning the object literal to a variable first bypasses excess property checks due to structural compatibility.
Does TypeScript check excess properties when assigning variables to variables?
ANo, it uses structural compatibility
BOnly if variables have different names
CYes, always
DOnly in strict mode
✗ Incorrect
TypeScript uses structural compatibility when assigning variables, so excess property checks do not apply.
Explain the difference between excess property checks and structural compatibility in TypeScript.
Think about when TypeScript warns about extra properties and when it allows them.
You got /4 concepts.
How can you handle a situation where an object literal has extra properties but you want to assign it to a type without errors?
Consider ways to bypass excess property checks safely.
You got /4 concepts.
Practice
(1/5)
1. What does TypeScript's excess property checks primarily prevent when assigning an object literal to a variable of a specific type?
easy
A. Assigning object literals with extra properties not defined in the target type
B. Assigning objects with missing properties
C. Assigning objects with incompatible property types
D. Assigning objects with properties in a different order
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 A
Quick Check:
Excess property checks = prevent extra properties [OK]
Hint: Excess checks block extra properties in direct object literals [OK]
Common Mistakes:
Confusing excess property checks with missing property errors
Thinking property order matters
Assuming excess checks apply to variables, not literals
2. Which of the following TypeScript assignments will cause an excess property check error?
easy
A. const obj: {name: string} = {name: 'Dave'} as {name: string};
B. const extra = {name: 'Bob', age: 25}; const obj: {name: string} = extra;
C. const obj: {name: string} = {name: 'Carol'};
D. const obj: {name: string} = {name: 'Alice', age: 30};
Solution
Step 1: Check direct object literal assignments
const obj: {name: string} = {name: 'Alice', age: 30}; assigns a literal with an extra property age directly 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.