0
0
Typescriptprogramming~20 mins

Extending interfaces in Typescript - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Interface Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of extended interface property access
What is the output of this TypeScript code when compiled and run with Node.js?
Typescript
interface Animal {
  name: string;
}

interface Dog extends Animal {
  breed: string;
}

const myDog: Dog = { name: "Buddy", breed: "Golden Retriever" };
console.log(myDog.name + " is a " + myDog.breed);
ABuddy is a undefined
BBuddy is a Golden Retriever
CError: Property 'breed' does not exist on type 'Dog'
Dundefined is a Golden Retriever
Attempts:
2 left
💡 Hint
Remember that extending an interface adds properties from the base interface.
Predict Output
intermediate
2:00remaining
Output when extending multiple interfaces
What will this TypeScript code output when run?
Typescript
interface A {
  x: number;
}
interface B {
  y: number;
}
interface C extends A, B {
  z: number;
}
const obj: C = { x: 1, y: 2, z: 3 };
console.log(obj.x + obj.y + obj.z);
A6
BNaN
C123
DError: Property 'y' does not exist on type 'C'
Attempts:
2 left
💡 Hint
Extending multiple interfaces combines their properties.
🔧 Debug
advanced
2:00remaining
Why does this extended interface cause a type error?
This TypeScript code causes a type error. What is the reason?
Typescript
interface Base {
  id: number;
}
interface Extended extends Base {
  id: string;
}
const obj: Extended = { id: "abc" };
AObject 'obj' must have both number and string types for 'id'
BMissing property 'id' in object literal
CInterface Extended must not extend Base
DCannot redeclare property 'id' with a different type in the extended interface
Attempts:
2 left
💡 Hint
Check if property types are compatible when extending interfaces.
📝 Syntax
advanced
2:00remaining
Identify the syntax error in interface extension
Which option contains the correct syntax to extend interface Person with interface Employee?
Ainterface Employee extends Person { salary: number; }
Binterface Employee Person extends { salary: number; }
Cinterface Employee : Person { salary: number; }
Dinterface Employee inherits Person { salary: number; }
Attempts:
2 left
💡 Hint
The keyword to extend an interface is 'extends'.
🚀 Application
expert
2:00remaining
Number of properties in extended interface object
Given these interfaces and object, how many properties does the object 'fullProfile' have?
Typescript
interface Contact {
  email: string;
}
interface Profile extends Contact {
  username: string;
  age: number;
}
const fullProfile: Profile = {
  email: "user@example.com",
  username: "user123",
  age: 30
};
A1
B2
C3
D4
Attempts:
2 left
💡 Hint
Count all properties from the base and extended interfaces present in the object.