Challenge - 5 Problems
Excess Property Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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);Attempts:
2 left
💡 Hint
Excess property checks apply only to object literals assigned directly to typed variables.
✗ Incorrect
When assigning an object literal directly to a variable typed as an interface, TypeScript performs excess property checks. The extra property 'location' is not allowed, causing a compile-time error.
❓ Predict Output
intermediate2: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);Attempts:
2 left
💡 Hint
Excess property checks do not apply when assigning from a variable.
✗ Incorrect
When assigning from a variable, TypeScript uses structural compatibility and does not check for excess properties. The assignment succeeds and logs 'Bob'.
🔧 Debug
advanced2: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 });Attempts:
2 left
💡 Hint
Check how object literals are checked when passed as function arguments.
✗ Incorrect
Passing an object literal with an extra property 'year' to a function expecting Car triggers excess property checks and causes a compile-time error.
❓ Predict Output
advanced2: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);Attempts:
2 left
💡 Hint
TypeScript allows extra properties when assigned from variables, but type annotations affect access.
✗ Incorrect
The variable 'b' is typed as Book, but the underlying object has 'pages'. JavaScript allows access to 'pages', so it logs 328.
🧠 Conceptual
expert2:00remaining
Why does excess property check not apply in this case?
Consider this TypeScript code:
Why does TypeScript allow this assignment without error?
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?
Attempts:
2 left
💡 Hint
Think about when excess property checks are enforced.
✗ Incorrect
Excess property checks only apply to object literals assigned directly to typed variables or passed as arguments. Assignments from variables skip this check and rely on structural compatibility.