Inheritance
Type Intersection
In TypeScript, type aliases can simulate inheritance by combining types using intersection (&) thus extending existing types.
This allows us to create more complex and specific types by building on top of existing ones. For instance, we can define a base type and then extend it with additional properties to create a more specialized type:
ts
// Define a base type aliastypePerson = {name : string;age : number;};// Extend the base type alias with additional propertiestypeEmployee =Person & {role : string;department : string;};// Example usageletemployee :Employee = {name : "John",age : 30,role : "Developer",department : "Engineering",};console .log (employee );// Output: { name: 'John', age: 30, role: 'Developer', department: 'Engineering' }
Here, Employee inherits the properties from Person and adds its own, allowing us to create a type hierarchy without needing classes. This approach provides a flexible and concise way to manage related types in our TypeScript code.
Interface
Interfaces can also extended. This means you can create a new interface that inherits the properties and methods from multiple existing interfaces.
This allows us to create complex types by combining simpler, more focused types.
ts
interfaceModel {year : number;}interfaceLicense {registration : string;}// Inherits properties from the previous interfacesinterfaceCar extendsModel ,License {passengers : number; // Adds a new property}// Inherits properties from the previous interfacesinterfaceTruck extendsModel ,License {capacity : string; // Adds a new property}constcar :Car = {passengers : 5,year : 2020,registration : "ABC 123",};constpickup :Truck = {capacity : "1.5 Ton",year : 2020,registration : "ABC 123",};
This makes our code more reusable. We can reuse existing interfaces to create new, more complex types without duplicating code. By combining multiple interfaces we can create flexible and modular type definitions.
It also ensures that any object or class implementing the extended interface adheres to all the combined properties and methods, reducing errors.
Using Class
and Interface
together
ts
interfaceModel {name : string;year : number;}interfaceHealth {condition : string;mileage : number;}// A Class can implement multiple interfaces.classCar implementsModel ,Health {name : string;year : number;condition : string;mileage : number;constructor(name : string,year : number,condition : string,mileage : number) {this.name =name ;this.year =year ;this.condition =condition ;this.mileage =mileage ;}getCarDetails () {return `Car: ${this.name }, Year: ${this.year },Condition: ${this.condition }, Mileage: ${this.mileage }`;}}
Using inheritance we can create powerful, type-safe abstractions in our TypeScript code, enhancing both code organization and maintainability.