0
0
Typescriptprogramming~20 mins

Multiple interface extension in Typescript - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Multiple Interface Extension Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of multiple interface extension with overlapping properties
What is the output of the following TypeScript code when compiled and run with Node.js (ignoring type errors)?
Typescript
interface A {
  x: number;
}
interface B {
  y: string;
}
interface C extends A, B {
  z: boolean;
}
const obj: C = { x: 10, y: "hello", z: true };
console.log(obj.x, obj.y, obj.z);
A10 undefined true
B10 hello true
Cundefined hello true
DCompilation error due to multiple interface extension
Attempts:
2 left
💡 Hint
Interfaces can extend multiple interfaces and combine their properties.
Predict Output
intermediate
2:00remaining
Property type conflict in multiple interface extension
What error or output occurs when the following TypeScript code is compiled?
Typescript
interface A {
  prop: number;
}
interface B {
  prop: string;
}
interface C extends A, B {}
const obj: C = { prop: 42 };
ANo error, obj.prop is string '42'
BNo error, obj.prop is number 42
CRuntime error due to conflicting types
DCompilation error: Interface 'C' cannot simultaneously extend types with conflicting property 'prop'.
Attempts:
2 left
💡 Hint
Check if properties with the same name but different types cause conflicts.
🔧 Debug
advanced
2:00remaining
Why does this multiple interface extension cause a type error?
Given the code below, why does TypeScript report an error on the assignment to obj?
Typescript
interface X {
  a: number;
}
interface Y {
  b: string;
}
interface Z extends X, Y {
  a: string;
}
const obj: Z = { a: "test", b: "hello" };
ABecause interface Z redeclares property 'a' with a different type than interface X, causing a conflict.
BBecause property 'b' is missing in obj.
CBecause interface Z cannot extend more than one interface.
DBecause obj is missing property 'a' of type number.
Attempts:
2 left
💡 Hint
Look at property 'a' in interfaces X and Z.
🚀 Application
advanced
2:00remaining
Designing an interface that extends multiple interfaces with optional properties
You want to create an interface 'Person' that extends interfaces 'Name' and 'Contact'. 'Name' has required properties 'first' and 'last'. 'Contact' has optional properties 'email' and 'phone'. Which interface definition correctly models this?
A
interface Person extends Name {
  email?: string;
  phone?: string;
}
B
interface Person extends Contact {
  first: string;
  last: string;
}
Cinterface Person extends Name, Contact {}
D
interface Person {
  first: string;
  last: string;
  email?: string;
  phone?: string;
}
Attempts:
2 left
💡 Hint
Interfaces can extend multiple interfaces to combine their properties.
🧠 Conceptual
expert
2:00remaining
Understanding property merging in multiple interface extension
Consider these interfaces: interface A { x: number; } interface B { x?: number; } interface C extends A, B {} What is the type of property 'x' in interface C?
A'x' is required and of type number
BCompilation error due to conflicting optionality
C'x' is required and of type number | undefined
D'x' is optional and of type number
Attempts:
2 left
💡 Hint
Required properties take precedence over optional ones when merging.