0
0
Typescriptprogramming~20 mins

Declaration merging for interfaces in Typescript - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Declaration Merging Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of merged interface properties
What is the output of the following TypeScript code when compiled and run with Node.js (using ts-node or similar)?
Typescript
interface User {
  name: string;
}
interface User {
  age: number;
}
const user: User = { name: "Alice", age: 30 };
console.log(user);
A{ name: 'Alice', age: 30 }
BTypeError: Cannot read property 'name' of undefined
CSyntaxError: Duplicate identifier 'User'
D{ name: 'Alice' }
Attempts:
2 left
💡 Hint
Think about how TypeScript merges interface declarations with the same name.
Predict Output
intermediate
2:00remaining
Merged interface method call output
What will be printed when running this TypeScript code?
Typescript
interface Logger {
  log(message: string): void;
}
interface Logger {
  error(message: string): void;
}
const logger: Logger = {
  log: (msg) => console.log('LOG:', msg),
  error: (msg) => console.error('ERROR:', msg)
};
logger.log('Hello');
logger.error('Oops');
ASyntaxError: Duplicate interface 'Logger'
BTypeError: logger.error is not a function
CLOG: Hello\nERROR: Oops
DLOG: Hello
Attempts:
2 left
💡 Hint
Interfaces with the same name combine their members.
🔧 Debug
advanced
2:30remaining
Why does this merged interface cause a type error?
Given the following code, why does TypeScript report an error on the variable 'person'?
Typescript
interface Person {
  name: string;
}
interface Person {
  name: number;
}
const person: Person = { name: 'Bob' };
ABecause the variable 'person' is missing the 'age' property.
BBecause the 'name' property has conflicting types (string and number) in merged interfaces.
CBecause interfaces cannot be declared twice with the same name.
DBecause the object literal is missing a type annotation.
Attempts:
2 left
💡 Hint
Check the types of the 'name' property in both interface declarations.
📝 Syntax
advanced
2:00remaining
Which interface merging syntax is invalid?
Which of the following interface declarations will cause a TypeScript syntax error due to invalid merging?
A
interface Settings { theme: string; }
interface Settings { theme: number; }
B
interface Config { url: string; }
interface Config { timeout?: number; }
C
interface Data { id: number; }
interface Data { value: string; }
D
interface Options { debug: boolean; }
interface Options { debug: boolean; }
Attempts:
2 left
💡 Hint
Look for conflicting property types in the same interface name.
🚀 Application
expert
3:00remaining
How many properties does the merged interface have?
Consider these interface declarations in TypeScript. How many properties does the merged interface 'Product' have?
Typescript
interface Product {
  id: number;
  name: string;
}
interface Product {
  description?: string;
  price: number;
}
interface Product {
  price: number;
  stock: number;
}
A4
B3
C6
D5
Attempts:
2 left
💡 Hint
Count unique property names after merging all interfaces.