What if you could add new powers to your code without rewriting everything from scratch?
Why Extending classes with types in Typescript? - Purpose & Use Cases
Imagine you have a basic class for a car, and you want to create a new class for electric cars that adds battery features. Doing this manually means copying all the car properties and methods, then adding new ones for the battery.
Copying code like this is slow and risky. If you fix a bug or add a feature in the original car class, you have to remember to update every copy. This causes errors and wastes time.
Extending classes with types lets you create a new class that automatically includes all features of the original class. You just add what's new. This keeps code clean, easy to update, and avoids mistakes.
class Car { drive() { console.log('Driving'); } } class ElectricCar { drive() { console.log('Driving'); } charge() { console.log('Charging'); } }
class Car { drive() { console.log('Driving'); } } class ElectricCar extends Car { charge() { console.log('Charging'); } }
You can build complex, organized programs by reusing and expanding existing code easily and safely.
Think of a video game where you have a basic character class. You can extend it to create a wizard class with magic powers without rewriting the whole character code.
Manual copying causes errors and wastes time.
Extending classes reuses code cleanly and safely.
It helps build bigger programs by adding new features easily.
Practice
extends keyword do in TypeScript class definitions?Solution
Step 1: Understand the role of
Theextendsextendskeyword is used to create a new class that inherits from an existing class, gaining its properties and methods.Step 2: Differentiate from other keywords
Other options describe unrelated concepts: interfaces, variables, or modules, which are not whatextendsdoes.Final Answer:
It creates a new class that inherits properties and methods from an existing class. -> Option CQuick Check:
extendsmeans inheritance [OK]
- Confusing extends with interface implementation
- Thinking extends imports modules
- Using extends to declare variables
Animal in TypeScript?Solution
Step 1: Recall correct syntax for class extension
In TypeScript, the keyword to extend a class isextends, so the syntax isclass Dog extends Animal {}.Step 2: Identify incorrect options
inheritsis not a TypeScript keyword,implementsis for interfaces, andimportis for modules.Final Answer:
class Dog extends Animal {} -> Option AQuick Check:
Useextendsto inherit classes [OK]
- Using 'inherits' instead of 'extends'
- Confusing 'implements' with 'extends'
- Trying to import classes inside class declaration
class Person {
name: string;
constructor(name: string) {
this.name = name;
}
greet() {
return `Hello, ${this.name}`;
}
}
class Student extends Person {
constructor(name: string) {
super(name);
}
}
const s = new Student('Anna');
console.log(s.greet());Solution
Step 1: Understand class inheritance and constructor call
TheStudentclass extendsPersonand callssuper(name)in its constructor, so thenameproperty is set correctly.Step 2: Check method call and output
Callings.greet()uses the inherited method, which returnsHello, Anna.Final Answer:
Hello, Anna -> Option BQuick Check:
super() sets parent properties correctly [OK]
- Forgetting to call super() causes errors
- Expecting child's own greet method without override
- Assuming name is undefined without super()
class Vehicle {
wheels: number;
constructor(wheels: number) {
this.wheels = wheels;
}
}
class Car extends Vehicle {
constructor() {
this.wheels = 4;
}
}Solution
Step 1: Check constructor in subclass
TheCarclass constructor assignsthis.wheels = 4but does not callsuper(), which is required before usingthis.Step 2: Understand TypeScript rules for subclass constructors
In TypeScript, a subclass constructor must callsuper()before accessingthis. Missing this causes a compile error.Final Answer:
Missing call to super() in Car constructor -> Option AQuick Check:
Always call super() before this in subclass constructor [OK]
- Assigning to this before super() call
- Forgetting super() call causes runtime error
- Thinking super() is optional in subclass constructor
Employee to add a department property and keep the name property from Person? class Person {
name: string;
constructor(name: string) {
this.name = name;
}
}
class Employee extends Person {
// Add department property here
}Solution
Step 1: Add department property and constructor parameters
To adddepartment, declare it and add it as a parameter in the constructor along withname.Step 2: Call super(name) and assign department
Callsuper(name)to set the inheritednameproperty, then assignthis.department = department.Final Answer:
class Employee extends Person { department: string; constructor(name: string, department: string) { super(name); this.department = department; } } -> Option DQuick Check:
Use super() for parent props and assign new props in constructor [OK]
- Not calling super() in constructor
- Missing department parameter in constructor
- Assigning this.department before super() call
Ready to prove it?
Pass the test and this pattern moves unknown
