Bird
Raised Fist0
Typescriptprogramming~20 mins

Excess property checking behavior in Typescript - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Excess Property Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this TypeScript code with excess property?
Consider the following TypeScript code snippet. What will be the output when compiled and run with strict excess property checking enabled?
Typescript
interface Person {
  name: string;
  age: number;
}

const p: Person = { name: "Alice", age: 30, location: "NY" };
console.log(p);
ACompilation warning but code runs and prints { name: 'Alice', age: 30 }
B{ name: 'Alice', age: 30, location: 'NY' } printed to console
CCompilation error: Object literal may only specify known properties, and 'location' does not exist in type 'Person'.
DRuntime error: Property 'location' is not defined on type 'Person'.
Attempts:
2 left
💡 Hint
Think about how TypeScript treats extra properties in object literals assigned to typed variables.
❓ Predict Output
intermediate
2:00remaining
What happens when assigning an object with excess properties via a variable?
Given this TypeScript code, what will be the output or error?
Typescript
interface Car {
  make: string;
  year: number;
}

const obj = { make: "Toyota", year: 2020, color: "red" };
const myCar: Car = obj;
console.log(myCar);
ARuntime error: 'color' property not allowed on type 'Car'.
BCompilation error due to excess property 'color'.
C{ make: 'Toyota', year: 2020 } printed to console, 'color' ignored.
D{ make: 'Toyota', year: 2020, color: 'red' } printed to console.
Attempts:
2 left
💡 Hint
Excess property checking applies only to object literals assigned directly to typed variables.
🔧 Debug
advanced
2:00remaining
Why does this TypeScript code cause an error with excess properties?
Identify the cause of the error in this code and select the correct explanation.
Typescript
interface Book {
  title: string;
  author: string;
}

function printBook(book: Book) {
  console.log(`${book.title} by ${book.author}`);
}

printBook({ title: "1984", author: "Orwell", pages: 328 });
AError because 'pages' is missing in the 'Book' interface.
BError because 'pages' is an excess property not defined in 'Book' interface.
CNo error; extra properties are allowed in function arguments.
DError because 'title' and 'author' are missing in the object.
Attempts:
2 left
💡 Hint
Check how TypeScript treats object literals passed directly as function arguments.
📝 Syntax
advanced
2:00remaining
Which option correctly bypasses excess property checking in TypeScript?
You want to assign an object with extra properties to a typed variable without error. Which option achieves this?
Typescript
interface User {
  username: string;
  email: string;
}

const user = { username: "john", email: "john@example.com", age: 25 };
Aconst u: User = user as User; // no error
Bconst u: User = { ...user }; // no error
Cconst u: User = user; // no error
Dconst u: User = { username: user.username, email: user.email }; // no error
Attempts:
2 left
💡 Hint
Type assertion can bypass excess property checks.
🚀 Application
expert
2:00remaining
How many properties does the resulting object have after this code runs?
Analyze the following TypeScript code and determine how many properties the final object 'result' has.
Typescript
interface Config {
  host: string;
  port: number;
}

const extra = { port: 8080, protocol: "https" };
const config: Config = { host: "localhost", ...extra };
const result = config;
ACompilation error due to excess property 'protocol'
B3 properties: 'host', 'port', and 'protocol'
C1 property: 'host' only
D2 properties: 'host' and 'port'
Attempts:
2 left
💡 Hint
Excess property checking applies to object literals, including properties from spread operators if they introduce unknown properties.

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.

Solution

  1. Step 1: Understand excess property checking purpose

    TypeScript checks object literals assigned to typed variables to catch unexpected properties early.
  2. Step 2: Behavior on extra properties

    If the object literal has properties not in the interface, TypeScript warns to prevent bugs.
  3. Final Answer:

    It warns if the object literal has properties not defined in the interface. -> Option C
  4. Quick Check:

    Excess property checking warns on extra keys [OK]
Hint: Extra keys in object literals cause warnings [OK]
Common Mistakes:
  • Thinking extra properties are removed automatically
  • Believing excess property checking applies to variables, not literals
  • Assuming runtime errors occur due to excess properties
2. Which of the following TypeScript code snippets will cause an excess property checking error?
easy
A. const obj: {name: string} = {name: "Alice", age: 30};
B. const obj = {name: "Alice", age: 30};
C. const obj: {name: string, age?: number} = {name: "Alice"};
D. const obj: {name: string} = {name: "Alice"};

Solution

  1. Step 1: Identify object literal with extra property

    const obj: {name: string} = {name: "Alice", age: 30}; assigns an object literal with an extra property 'age' to a type expecting only 'name'.
  2. Step 2: Check excess property rules

    TypeScript flags extra properties in object literals when they don't exist in the target type.
  3. Final Answer:

    const obj: {name: string} = {name: "Alice", age: 30}; -> Option A
  4. Quick Check:

    Extra property 'age' causes error [OK]
Hint: Extra keys in typed object literals cause errors [OK]
Common Mistakes:
  • Confusing object literals with variables
  • Ignoring optional properties
  • Assuming all extra properties are allowed
3. What will be the output of the following TypeScript code?
interface Person { name: string; age: number; }
const p: Person = { name: "Bob", age: 25, city: "NY" };
medium
A. Compilation error due to excess property 'city'.
B. Object p is created with all three properties.
C. Runtime error because 'city' is not in Person.
D. Only 'name' and 'age' are assigned; 'city' is ignored.

Solution

  1. Step 1: Check object literal assignment to interface

    The object literal has an extra property 'city' not defined in Person.
  2. Step 2: Apply excess property checking rules

    TypeScript raises a compile-time error for extra properties in object literals assigned to typed variables.
  3. Final Answer:

    Compilation error due to excess property 'city'. -> Option A
  4. Quick Check:

    Extra property in literal causes compile error [OK]
Hint: Extra keys in object literals cause compile errors [OK]
Common Mistakes:
  • Thinking extra keys are ignored silently
  • Expecting runtime errors instead of compile errors
  • Assuming object creation succeeds with extra keys
4. Consider this TypeScript code:
interface Car { make: string; model: string; }
const myCar = { make: "Toyota", model: "Camry", year: 2020 };
const car: Car = myCar;

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

  1. Step 1: Identify assignment type

    The variable 'myCar' is assigned to 'car', not an object literal directly.
  2. Step 2: Understand excess property checking scope

    Excess property checking only triggers on object literals, not on variables or expressions.
  3. Final Answer:

    Because excess property checking only applies to object literals, not variables. -> Option B
  4. 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 };
B. const user: { username: string } = { username: "john", age: 30 };
C. const user: User = { username: "john" }; user.age = 30;
D. const user: User = { username: "john", age: 30 } as User;

Solution

  1. Step 1: Understand excess property checking on object literals

    Assigning an object literal with extra properties directly causes errors.
  2. Step 2: Use type assertion to bypass excess property checking

    Using 'as User' tells TypeScript to trust the type, skipping excess property checks.
  3. Step 3: Analyze options

    const user: User = { username: "john", age: 30 } as User; uses type assertion on the object literal, avoiding error. const user: { username: string } = { username: "john", age: 30 }; causes excess property error. const user: User = { username: "john" }; user.age = 30; tries to add property after but TypeScript errors: Property 'age' does not exist on type 'User'. const user: User = { username: "john", age: 30 }; causes excess property error.
  4. Final Answer:

    const user: User = { username: "john", age: 30 } as User; -> Option D
  5. Quick Check:

    Type assertion on literal skips excess check [OK]
Hint: Use 'as Type' to bypass excess property errors [OK]
Common Mistakes:
  • Trying to assign extra properties directly without assertion
  • Asserting variable instead of object literal
  • Adding extra properties after assignment without type update
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard