Bird
Raised Fist0
Typescriptprogramming~20 mins

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

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

Practice

(1/5)
1. What happens when two interfaces with the same name are declared in TypeScript?
easy
A. Their properties merge into a single interface.
B. The second interface overwrites the first one.
C. A syntax error occurs due to duplicate names.
D. Only the first interface is used, others ignored.

Solution

  1. Step 1: Understand interface declaration merging

    In TypeScript, interfaces with the same name combine their properties into one interface.
  2. Step 2: Effect of merging

    This allows extending interfaces without overwriting or errors.
  3. Final Answer:

    Their properties merge into a single interface. -> Option A
  4. Quick Check:

    Declaration merging = properties combined [OK]
Hint: Same name interfaces combine properties automatically [OK]
Common Mistakes:
  • Thinking second interface overwrites the first
  • Expecting a syntax error on duplicate names
  • Assuming only the first interface is used
2. Which of the following is the correct syntax to merge two interfaces named Person in TypeScript?
easy
A. class Person { name: string } class Person { age: number }
B. interface Person { name: string } interface Person { age: number }
C. type Person = { name: string } type Person = { age: number }
D. interface Person { name: string } type Person = { age: number }

Solution

  1. Step 1: Identify valid interface merging syntax

    Only multiple interface declarations with the same name merge properties.
  2. Step 2: Check each option

    interface Person { name: string } interface Person { age: number } uses two interface Person declarations, which is correct. Options B and C mix types and interfaces or duplicate types, which do not merge. class Person { name: string } class Person { age: number } uses classes, which do not merge.
  3. Final Answer:

    interface Person { name: string } interface Person { age: number } -> Option B
  4. Quick Check:

    Multiple interface declarations = merge [OK]
Hint: Only interfaces with same name merge, not types or classes [OK]
Common Mistakes:
  • Trying to merge types instead of interfaces
  • Using classes instead of interfaces
  • Mixing type and interface declarations
3. What will be the output of the following TypeScript code?
interface Car { brand: string; }
interface Car { year: number; }
const myCar: Car = { brand: "Toyota", year: 2020 };
console.log(myCar.year);
medium
A. undefined
B. Runtime error
C. Compilation error
D. 2020

Solution

  1. Step 1: Understand interface merging effect

    The two Car interfaces merge their properties, so Car has both brand and year.
  2. Step 2: Analyze object and output

    The object myCar correctly has both properties. Logging myCar.year outputs 2020.
  3. Final Answer:

    2020 -> Option D
  4. Quick Check:

    Interface merge allows all properties [OK]
Hint: Merged interfaces combine properties, so all exist in object [OK]
Common Mistakes:
  • Expecting a compilation error due to duplicate interface
  • Thinking only first interface properties exist
  • Confusing runtime and compile-time errors
4. Identify the error in the following TypeScript code that attempts interface merging:
interface User { id: number; }
interface User { name: string; }
const user: User = { id: 1 };
medium
A. Cannot assign number to 'id' property.
B. Duplicate interface declarations cause syntax error.
C. Missing property 'name' in object 'user'.
D. Interface merging does not work with 'const' declarations.

Solution

  1. Step 1: Check interface merging and properties

    The two User interfaces merge, so User requires both id and name.
  2. Step 2: Analyze object assignment

    The object user only has id, missing name, causing a type error.
  3. Final Answer:

    Missing property 'name' in object 'user'. -> Option C
  4. Quick Check:

    All merged properties required in object [OK]
Hint: Merged interfaces require all properties in objects [OK]
Common Mistakes:
  • Thinking duplicate interfaces cause syntax errors
  • Ignoring missing properties in merged interface
  • Assuming 'const' affects interface merging
5. Given two interface declarations:
interface Product { id: number; }
interface Product { description?: string; price: number; }

Which of the following objects correctly implements the merged Product interface?
hard
A. { id: 101, price: 29.99 }
B. { id: 101, description: "Book" }
C. { price: 29.99, description: "Book" }
D. { id: 101 }

Solution

  1. Step 1: Combine interface properties

    The merged Product interface requires id (number) and price (number), with optional description.
  2. Step 2: Check each object against required properties

    { id: 101, price: 29.99 } has id and price, valid. { id: 101, description: "Book" } misses price. { price: 29.99, description: "Book" } misses id. { id: 101 } misses price.
  3. Final Answer:

    { id: 101, price: 29.99 } -> Option A
  4. Quick Check:

    Required properties must be present [OK]
Hint: All required merged properties must be in object [OK]
Common Mistakes:
  • Ignoring required properties when optional ones exist
  • Assuming optional properties are required
  • Missing required properties in object
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