Bird
Raised Fist0
Typescriptprogramming~20 mins

Excess property checks vs structural compatibility in Typescript - Practice Questions

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
Output of excess property check with object literal
What will be the output of this TypeScript code when compiled and run in JavaScript?
interface Person {
  name: string;
  age: number;
}

const p1: Person = { name: "Alice", age: 30, location: "NY" };
console.log(p1.name);
Typescript
interface Person {
  name: string;
  age: number;
}

const p1: Person = { name: "Alice", age: 30, location: "NY" };
console.log(p1.name);
ALogs 'undefined' to the console
BLogs 'Alice' to the console
CRuntime error: 'location' is not defined
DCompilation error due to excess property 'location'
Attempts:
2 left
💡 Hint
Excess property checks apply only to object literals assigned directly to typed variables.
❓ Predict Output
intermediate
2:00remaining
Structural compatibility with extra properties in variable
What will be the output of this TypeScript code when compiled and run in JavaScript?
interface Person {
  name: string;
  age: number;
}

const extra = { name: "Bob", age: 25, location: "LA" };
const p2: Person = extra;
console.log(p2.name);
Typescript
interface Person {
  name: string;
  age: number;
}

const extra = { name: "Bob", age: 25, location: "LA" };
const p2: Person = extra;
console.log(p2.name);
ACompilation error due to excess property 'location'
BLogs 'Bob' to the console
CLogs 'undefined' to the console
DRuntime error: 'location' is not defined
Attempts:
2 left
💡 Hint
Excess property checks do not apply when assigning from a variable.
🔧 Debug
advanced
2:00remaining
Identify the error in excess property check
Why does this TypeScript code produce an error?
interface Car {
  make: string;
  model: string;
}

function printCar(car: Car) {
  console.log(car.make + " " + car.model);
}

printCar({ make: "Toyota", model: "Corolla", year: 2020 });
Typescript
interface Car {
  make: string;
  model: string;
}

function printCar(car: Car) {
  console.log(car.make + " " + car.model);
}

printCar({ make: "Toyota", model: "Corolla", year: 2020 });
AError: Object literal has excess property 'year'
BNo error, code runs and logs 'Toyota Corolla'
CError: Missing property 'year' in interface Car
DRuntime error: 'year' is undefined
Attempts:
2 left
💡 Hint
Check how object literals are checked when passed as function arguments.
❓ Predict Output
advanced
2:00remaining
Output when assigning object with extra properties via variable
What will be the output of this TypeScript code when compiled and run in JavaScript?
interface Book {
  title: string;
  author: string;
}

const novel = { title: "1984", author: "Orwell", pages: 328 };
const b: Book = novel;
console.log(b.pages);
Typescript
interface Book {
  title: string;
  author: string;
}

const novel = { title: "1984", author: "Orwell", pages: 328 };
const b: Book = novel;
console.log(b.pages);
ALogs 328 to the console
BCompilation error due to excess property 'pages'
CLogs 'undefined' to the console
DRuntime error: Property 'pages' does not exist on type 'Book'
Attempts:
2 left
💡 Hint
TypeScript allows extra properties when assigned from variables, but type annotations affect access.
🧠 Conceptual
expert
2:00remaining
Why does excess property check not apply in this case?
Consider this TypeScript code:
interface User {
  username: string;
  email: string;
}

const data = { username: "user1", email: "user1@example.com", isAdmin: true };
const user: User = data;

Why does TypeScript allow this assignment without error?
ABecause TypeScript ignores extra properties in all assignments
BBecause 'isAdmin' is optional in the User interface
CBecause 'data' is a variable, excess property checks are skipped and structural compatibility applies
DBecause the interface User explicitly allows extra properties
Attempts:
2 left
💡 Hint
Think about when excess property checks are enforced.

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

  1. Step 1: Understand excess property checks context

    Excess property checks happen when assigning an object literal directly to a typed variable.
  2. 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.
  3. Final Answer:

    Assigning object literals with extra properties not defined in the target type -> Option A
  4. 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

  1. 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.
  2. 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.
  3. Final Answer:

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

    Direct literal with extra property = error [OK]
Hint: Extra properties in direct literals cause errors, variables do not [OK]
Common Mistakes:
  • Thinking variables cause excess property errors
  • Ignoring the difference between literals and variables
  • Assuming type assertions always cause errors
3. What will be the output of this TypeScript code?
interface Person { name: string; }
const extra = { name: 'Eve', age: 40 };
const p: Person = extra;
console.log(p.name);
medium
A. Runtime error
B. Error: Excess property 'age' not allowed
C. Eve
D. undefined

Solution

  1. Step 1: Understand assignment from variable

    Assigning from variable extra bypasses excess property checks, so assignment is allowed.
  2. Step 2: Check console output

    Accessing p.name prints 'Eve' as expected.
  3. Final Answer:

    Eve -> Option C
  4. Quick Check:

    Variable assignment allows extra properties, prints name [OK]
Hint: Variables with extra props assign fine; literals with extras error [OK]
Common Mistakes:
  • Expecting excess property error on variable assignment
  • Confusing compile-time error with runtime output
  • Assuming extra properties are removed
4. Identify the error in this TypeScript code and fix it:
interface Car { make: string; }
const car = { make: 'Toyota', year: 2020 };
const myCar: Car = { make: 'Honda', year: 2018 };
medium
A. Error due to extra property 'year' in direct object literal; fix by assigning from variable
B. Error due to missing property 'year'; fix by adding 'year' to interface
C. No error; code is correct
D. Error due to wrong property type; fix by changing 'year' to string

Solution

  1. Step 1: Identify excess property check error

    Direct assignment of object literal with extra property year causes error.
  2. Step 2: Fix by assigning from variable

    Assigning from variable car avoids excess property checks and fixes error.
  3. Final Answer:

    Error due to extra property 'year' in direct object literal; fix by assigning from variable -> Option A
  4. Quick Check:

    Direct literal with extra prop = error; use variable [OK]
Hint: Assign extra props via variable, not direct literal [OK]
Common Mistakes:
  • Adding extra properties to interface unnecessarily
  • Ignoring excess property check rules
  • Assuming no error on direct literal with extra props
5. Given this interface and code, which assignment will NOT cause a TypeScript error?
interface User { username: string; }

// Option 1
const u1: User = { username: 'john', email: 'john@example.com' };

// Option 2
const extraUser = { username: 'jane', email: 'jane@example.com' };
const u2: User = extraUser;

// Option 3
const u3 = { username: 'doe', email: 'doe@example.com' } as User;

// Option 4
const u4: User = { username: 'max' };
hard
A. Only Option 3 and Option 4
B. Only Option 2 and Option 4
C. Only Option 1 and Option 3
D. All options cause errors

Solution

  1. Step 1: Analyze Option 1

    Direct literal with extra property email causes excess property check error.
  2. Step 2: Analyze Option 2 and 4

    Option 2 assigns from variable, so no error. Option 4 matches interface exactly, no error.
  3. Step 3: Analyze Option 3

    Type assertion on direct object literal with extra property email still triggers excess property checks, causing an error.
  4. Final Answer:

    Only Option 2 and Option 4 -> Option B
  5. Quick Check:

    Direct literal with extras errors; variable assignment and exact match okay [OK]
Hint: Direct literals with extras error; variables and exact matches okay [OK]
Common Mistakes:
  • Thinking type assertions bypass excess property checks on object literals
  • Thinking all direct literals with extras are allowed
  • Ignoring structural compatibility rules
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