Excess property checking is a TypeScript feature that warns you when you provide extra properties in an object literal that are not expected by the target type.
Click to reveal answer
intermediate
Why does TypeScript perform excess property checking only on object literals?
TypeScript checks object literals for extra properties to catch typos or mistakes early. It does not check variables or objects assigned from elsewhere to allow flexibility.
Click to reveal answer
intermediate
How can you avoid excess property checking errors when passing objects with extra properties?
You can assign the object to a variable first, or use type assertions to tell TypeScript to trust your object shape and skip excess property checks.
Click to reveal answer
beginner
What happens if you pass an object literal with an extra property to a function expecting a specific type?
TypeScript will give an error about the extra property because it does excess property checking on object literals to prevent mistakes.
Click to reveal answer
advanced
Can excess property checking be bypassed by using index signatures in TypeScript interfaces?
Yes, if an interface has an index signature like [key: string]: any, excess property checking is relaxed because extra properties are allowed.
Click to reveal answer
When does TypeScript perform excess property checking?
AOn all objects, including variables and function returns
BOnly on arrays
COnly on object literals assigned directly to variables or parameters
DOnly on primitive types
✗ Incorrect
TypeScript performs excess property checking only on object literals directly assigned to variables or passed as parameters.
What error will TypeScript give if you pass an object literal with an unexpected property to a function expecting a specific type?
AExcess property error
BSyntax error
CRuntime error
DNo error
✗ Incorrect
TypeScript will report an excess property error to warn about unexpected properties in object literals.
How can you avoid excess property checking errors when you want to pass extra properties?
AUse a number instead of an object
BUse a different programming language
CRemove all properties
DAssign the object to a variable before passing it
✗ Incorrect
Assigning the object to a variable first disables excess property checking on that variable.
What effect does an index signature have on excess property checking?
AIt disables excess property checking for extra properties
BIt causes more errors
CIt only allows numeric keys
DIt has no effect
✗ Incorrect
An index signature allows extra properties, so excess property checking is relaxed.
Which of these is a common reason for excess property checking errors?
APassing a number instead of an object
BA typo in a property name in an object literal
CUsing a variable instead of an object literal
DUsing a function instead of an object
✗ Incorrect
Typos in property names cause extra properties that TypeScript flags during excess property checking.
Explain what excess property checking is and why TypeScript uses it.
Think about how TypeScript helps catch typos in object properties.
You got /3 concepts.
Describe two ways to avoid excess property checking errors when passing objects with extra properties.
Consider how you can tell TypeScript to trust your object shape.
You got /3 concepts.
Practice
(1/5)
1. What does excess property checking in TypeScript do when you assign an object literal to a variable typed with an interface?
easy
A. It automatically removes extra properties from the object.
B. It ignores any extra properties without warnings.
C. It warns if the object literal has properties not defined in the interface.
D. It throws a runtime error if extra properties exist.
Why does this code NOT cause an excess property checking error?
medium
A. Because TypeScript ignores extra properties in all assignments.
B. Because excess property checking only applies to object literals, not variables.
C. Because 'year' is an optional property in Car.
D. Because 'myCar' is explicitly typed as Car.
Solution
Step 1: Identify assignment type
The variable 'myCar' is assigned to 'car', not an object literal directly.
Step 2: Understand excess property checking scope
Excess property checking only triggers on object literals, not on variables or expressions.
Final Answer:
Because excess property checking only applies to object literals, not variables. -> Option B
Quick Check:
Excess check skips variables [OK]
Hint: Excess check triggers only on object literals [OK]
Common Mistakes:
Assuming optional properties cause errors
Thinking all assignments trigger excess checks
Believing extra properties cause runtime errors
5. You want to assign an object literal with extra properties to a variable typed by an interface without errors. Which approach correctly avoids excess property checking errors?
interface User { username: string; }
// Which option works without error?
hard
A. const user: User = { username: "john", age: 30 };