Bird
Raised Fist0
Typescriptprogramming~20 mins

Extending interfaces in Typescript - Practice Problems & Coding Challenges

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

Practice

(1/5)
1. What does the extends keyword do when used with interfaces in TypeScript?
easy
A. It merges two unrelated interfaces without inheritance.
B. It deletes properties from an existing interface.
C. It creates a new interface that inherits properties from an existing interface.
D. It converts an interface into a class.

Solution

  1. Step 1: Understand the role of extends in interfaces

    The extends keyword allows one interface to inherit properties from another, creating a new interface that includes all properties of the original.
  2. Step 2: Compare options with this understanding

    Only It creates a new interface that inherits properties from an existing interface. correctly describes inheritance of properties. Other options describe unrelated or incorrect behaviors.
  3. Final Answer:

    It creates a new interface that inherits properties from an existing interface. -> Option C
  4. Quick Check:

    Extending interfaces means inheritance = D [OK]
Hint: Remember: extends means inherit properties [OK]
Common Mistakes:
  • Thinking extends deletes properties
  • Confusing interfaces with classes
  • Believing extends merges unrelated interfaces
2. Which of the following is the correct syntax to extend an interface Person with a new interface Employee?
easy
A. interface Employee extends Person {}
B. interface Employee inherits Person {}
C. interface Employee : Person {}
D. interface Employee < Person {}

Solution

  1. Step 1: Recall TypeScript syntax for extending interfaces

    The correct keyword to extend an interface is extends, used as interface NewInterface extends ExistingInterface {}.
  2. Step 2: Check each option

    interface Employee extends Person {} uses the correct syntax. Options B, C, and D use invalid keywords or symbols.
  3. Final Answer:

    interface Employee extends Person {} -> Option A
  4. Quick Check:

    Use 'extends' keyword for interface inheritance [OK]
Hint: Use 'extends' keyword exactly for interfaces [OK]
Common Mistakes:
  • Using 'inherits' instead of 'extends'
  • Using ':' like in classes
  • Using symbols like '<' incorrectly
3. Given the code below, what is the type of employee?
interface Person {
  name: string;
}
interface Employee extends Person {
  salary: number;
}
const employee: Employee = { name: "Alice", salary: 50000 };
medium
A. { name: string; salary: number; }
B. { salary: number; }
C. { name: string; }
D. Error: Cannot assign object to Employee

Solution

  1. Step 1: Understand interface extension effect

    The Employee interface extends Person, so it includes all properties of Person plus its own.
  2. Step 2: Analyze the assigned object

    The object has both name (string) and salary (number), matching the combined interface.
  3. Final Answer:

    { name: string; salary: number; } -> Option A
  4. Quick Check:

    Extended interface combines all properties = A [OK]
Hint: Extended interfaces combine all properties [OK]
Common Mistakes:
  • Thinking only base interface properties apply
  • Ignoring added properties in extension
  • Expecting a type error incorrectly
4. Identify the error in the following TypeScript code:
interface Animal {
  species: string;
}
interface Dog extends Animal {
  breed: string;
}
const pet: Dog = { species: "Canine" };
medium
A. Property 'species' should be a number, not string.
B. Cannot extend interface 'Animal' with 'Dog'.
C. Syntax error in interface declaration.
D. Missing property 'breed' in object assigned to 'pet'.

Solution

  1. Step 1: Check properties required by extended interface

    The Dog interface requires both species and breed properties.
  2. Step 2: Verify object assigned to pet

    The object only has species, missing breed, causing a type error.
  3. Final Answer:

    Missing property 'breed' in object assigned to 'pet'. -> Option D
  4. Quick Check:

    All extended interface properties must be present [OK]
Hint: Check all properties from base and extended interfaces [OK]
Common Mistakes:
  • Ignoring missing properties in assigned object
  • Thinking extending interfaces causes syntax errors
  • Confusing property types
5. You have two interfaces:
interface Vehicle {
  make: string;
  model: string;
}
interface ElectricVehicle extends Vehicle {
  batteryCapacity: number;
}

How would you create a new interface ElectricCar that extends ElectricVehicle and adds a range property (number)?
hard
A. interface ElectricCar extends Vehicle ElectricVehicle { range: number; }
B. interface ElectricCar extends ElectricVehicle { range: number; }
C. interface ElectricCar implements ElectricVehicle { range: number; }
D. interface ElectricCar extends Vehicle { batteryCapacity: number; range: number; }

Solution

  1. Step 1: Understand interface extension chain

    ElectricVehicle already extends Vehicle, so extending ElectricVehicle includes all properties from both.
  2. Step 2: Define ElectricCar extending ElectricVehicle and add range

    The correct syntax is interface ElectricCar extends ElectricVehicle { range: number; }.
  3. Step 3: Evaluate other options

    interface ElectricCar extends Vehicle ElectricVehicle { range: number; } has invalid syntax (missing comma). interface ElectricCar implements ElectricVehicle { range: number; } uses implements which is invalid for interfaces. interface ElectricCar extends Vehicle { batteryCapacity: number; range: number; } misses extending ElectricVehicle and manually adds properties.
  4. Final Answer:

    interface ElectricCar extends ElectricVehicle { range: number; } -> Option B
  5. Quick Check:

    Extend the most specific interface and add new properties [OK]
Hint: Extend the most specific interface, add new properties [OK]
Common Mistakes:
  • Trying to extend multiple interfaces unnecessarily
  • Using 'implements' instead of 'extends' for interfaces
  • Manually repeating inherited properties
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