0
0
Typescriptprogramming~20 mins

Excess property checks vs structural compatibility in Typescript - Practice Questions

Choose your learning style9 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.