📖 Scenario: Imagine you are creating a simple app to manage user profiles. Each user profile should have a name and age. Sometimes, extra information might be added by mistake. TypeScript helps catch these mistakes with excess property checking.
🎯 Goal: You will create a user profile object, add a configuration variable, use a function to accept only allowed properties, and finally print the user profile to see how TypeScript handles extra properties.
📋 What You'll Learn
Create an interface UserProfile with name as string and age as number
Create a variable user with name, age, and an extra property location
Create a boolean variable allowExtra set to false
Write a function createUser that accepts only UserProfile type
Call createUser with user and print the result
💡 Why This Matters
🌍 Real World
In real apps, you often receive data with extra fields. Understanding excess property checking helps you catch mistakes early and keep your data clean.
💼 Career
Many jobs require writing safe TypeScript code. Knowing how to handle excess properties is important for building reliable applications and avoiding bugs.
Progress0 / 4 steps
1
Create the UserProfile interface and user object
Create an interface called UserProfile with properties name (string) and age (number). Then create a variable called user with name set to "Alice", age set to 30, and an extra property location set to "Wonderland".
Typescript
Hint
Use interface UserProfile { name: string; age: number; } and then create const user = { name: "Alice", age: 30, location: "Wonderland" }.
2
Add a configuration variable
Create a boolean variable called allowExtra and set it to false.
Typescript
Hint
Just write const allowExtra = false; below the user object.
3
Write a function to accept only UserProfile type
Write a function called createUser that takes one parameter profile of type UserProfile and returns it.
Typescript
Hint
Define function createUser(profile: UserProfile): UserProfile { return profile; }.
4
Call createUser and print the result
Call the function createUser with the variable user and print the result using console.log.
Typescript
Hint
Write console.log(createUser(user)); to see the output.
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 };