What if you could fix one place and update all related types instantly?
Why Extending interfaces in Typescript? - Purpose & Use Cases
Imagine you have several objects representing different types of vehicles, each with some shared properties like make and model, but also some unique ones. You try to write separate type definitions for each, repeating the shared parts over and over.
This manual repetition is slow and error-prone. If you want to update the shared properties, you must change every type separately. It's easy to forget one, causing bugs and inconsistent code.
Extending interfaces lets you create a base interface with shared properties, then build new interfaces that add or change details. This keeps your code clean, consistent, and easy to update.
interface Car { make: string; model: string; doors: number; }
interface Truck { make: string; model: string; capacity: number; }interface Vehicle { make: string; model: string; }
interface Car extends Vehicle { doors: number; }
interface Truck extends Vehicle { capacity: number; }You can build complex, organized type structures that grow with your app without repeating yourself or risking mistakes.
Think of a delivery app where you track cars, trucks, and bikes. Each vehicle shares some info, but also has unique details. Extending interfaces helps you model this clearly and safely.
Extending interfaces avoids repeating shared properties.
It makes updating types easier and safer.
It helps organize related data clearly in your code.
Practice
extends keyword do when used with interfaces in TypeScript?Solution
Step 1: Understand the role of
Theextendsin interfacesextendskeyword allows one interface to inherit properties from another, creating a new interface that includes all properties of the original.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.Final Answer:
It creates a new interface that inherits properties from an existing interface. -> Option CQuick Check:
Extending interfaces means inheritance = D [OK]
- Thinking extends deletes properties
- Confusing interfaces with classes
- Believing extends merges unrelated interfaces
Person with a new interface Employee?Solution
Step 1: Recall TypeScript syntax for extending interfaces
The correct keyword to extend an interface isextends, used asinterface NewInterface extends ExistingInterface {}.Step 2: Check each option
interface Employee extends Person {} uses the correct syntax. Options B, C, and D use invalid keywords or symbols.Final Answer:
interface Employee extends Person {} -> Option AQuick Check:
Use 'extends' keyword for interface inheritance [OK]
- Using 'inherits' instead of 'extends'
- Using ':' like in classes
- Using symbols like '<' incorrectly
employee?
interface Person {
name: string;
}
interface Employee extends Person {
salary: number;
}
const employee: Employee = { name: "Alice", salary: 50000 };Solution
Step 1: Understand interface extension effect
TheEmployeeinterface extendsPerson, so it includes all properties ofPersonplus its own.Step 2: Analyze the assigned object
The object has bothname(string) andsalary(number), matching the combined interface.Final Answer:
{ name: string; salary: number; } -> Option AQuick Check:
Extended interface combines all properties = A [OK]
- Thinking only base interface properties apply
- Ignoring added properties in extension
- Expecting a type error incorrectly
interface Animal {
species: string;
}
interface Dog extends Animal {
breed: string;
}
const pet: Dog = { species: "Canine" };Solution
Step 1: Check properties required by extended interface
TheDoginterface requires bothspeciesandbreedproperties.Step 2: Verify object assigned to
The object only haspetspecies, missingbreed, causing a type error.Final Answer:
Missing property 'breed' in object assigned to 'pet'. -> Option DQuick Check:
All extended interface properties must be present [OK]
- Ignoring missing properties in assigned object
- Thinking extending interfaces causes syntax errors
- Confusing property types
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)?Solution
Step 1: Understand interface extension chain
ElectricVehiclealready extendsVehicle, so extendingElectricVehicleincludes all properties from both.Step 2: Define
The correct syntax isElectricCarextendingElectricVehicleand addrangeinterface ElectricCar extends ElectricVehicle { range: number; }.Step 3: Evaluate other options
interface ElectricCar extends Vehicle ElectricVehicle { range: number; } has invalid syntax (missing comma). interface ElectricCar implements ElectricVehicle { range: number; } usesimplementswhich is invalid for interfaces. interface ElectricCar extends Vehicle { batteryCapacity: number; range: number; } misses extendingElectricVehicleand manually adds properties.Final Answer:
interface ElectricCar extends ElectricVehicle { range: number; } -> Option BQuick Check:
Extend the most specific interface and add new properties [OK]
- Trying to extend multiple interfaces unnecessarily
- Using 'implements' instead of 'extends' for interfaces
- Manually repeating inherited properties
Ready to prove it?
Pass the test and this pattern moves unknown
