📖 Scenario: Imagine you are creating a simple system to describe different types of vehicles. Each vehicle has some common features, but some vehicles have extra features. You want to use TypeScript interfaces to organize these features clearly.
🎯 Goal: You will build TypeScript interfaces where one interface extends multiple other interfaces. This helps combine features from different sources into one interface.
📋 What You'll Learn
Create interfaces with specific properties
Use multiple interface extension
Create an object that uses the extended interface
Print the combined object properties
💡 Why This Matters
🌍 Real World
Using multiple interface extension helps organize complex data models in software, like describing vehicles with many features.
💼 Career
Understanding interface extension is important for TypeScript developers to write clean, reusable, and scalable code.
Progress0 / 4 steps
1
Create base interfaces
Create two interfaces called Engine and Wheels. The Engine interface should have a property horsepower of type number. The Wheels interface should have a property count of type number.
Typescript
Hint
Use the interface keyword to create interfaces. Each property needs a name and a type.
2
Create an interface that extends both Engine and Wheels
Create an interface called Car that extends both Engine and Wheels. Add a new property brand of type string to Car.
Typescript
Hint
Use extends with a comma to extend multiple interfaces.
3
Create a variable of type Car
Create a variable called myCar of type Car. Assign it an object with horsepower 150, count 4, and brand "Toyota".
Typescript
Hint
Use const to create the variable and assign an object with all required properties.
4
Print the car details
Write a console.log statement to print the string: "My Toyota car has 150 horsepower and 4 wheels." using the properties of myCar.
Typescript
Hint
Use a template string with backticks and ${} to insert variables inside the string.
Practice
(1/5)
1. What does interface C extends A, B mean in TypeScript?
easy
A. Interface C is unrelated to interfaces A and B.
B. Interface C can only have properties from A or B, not both.
C. Interface C replaces interfaces A and B completely.
D. Interface C includes all properties from interfaces A and B.
Solution
Step 1: Understand interface extension
When an interface extends others, it inherits all their properties.
Step 2: Apply to multiple interfaces
Extending both A and B means C has all properties from both interfaces combined.
Final Answer:
Interface C includes all properties from interfaces A and B. -> Option D