Introduction to Extend Interface in TypeScript
TypeScript is an open-source programming language that is an extension of JavaScript. It is designed to develop large-scale applications. TypeScript is a statically typed language that compiles to plain JavaScript code, which can be executed on any browser or platform that supports JavaScript.
One of the many features of TypeScript is the ability to extend interfaces, which is similar to class inheritance. This feature allows the creation of new interfaces that inherit the properties and methods of existing interfaces and add additional properties or methods. In this article, we will explore the concept of extending interfaces in TypeScript and how it can be used to improve the development experience.
How to Extend an Interface in TypeScript
To extend an interface in TypeScript, we use the `extends` keyword followed by the name of the interface we want to extend. Here is an example of how to extend an interface:
“`typescript
interface Person {
name: string;
age: number;
}
interface Employee extends Person {
employeeId: number;
department: string;
}
“`
In this example, we created an interface called `Person` with properties `name` and `age`. Then we created another interface called `Employee` that extends the `Person` interface and adds `employeeId` and `department` properties. Now we can use the `Employee` interface to declare employee objects that have all the properties of a person object plus the additional properties of an employee object.
Accessing Properties and Methods of Extended Interfaces
When we extend an interface, we inherit all the properties and methods of that interface. We can access these properties and methods using dot notation. Here is an example:
“`typescript
interface Person {
name: string;
age: number;
}
interface Employee extends Person {
employeeId: number;
department: string;
}
let john: Employee = {
name: “John”,
age: 25,
employeeId: 123,
department: “IT”,
};
console.log(john.employeeId); // Output: 123
console.log(john.age); // Output: 25
“`
In this example, we declared an employee object using the `Employee` interface. We initialized the `name`, `age`, `employeeId`, and `department` properties of the `john` object. We can access the `employeeId` property using dot notation, and we can also access the `age` property inherited from the `Person` interface.
Overriding Properties and Methods of Extended Interfaces
When we extend an interface, we can override its properties and methods. Here is an example of how to override properties and methods of an interface:
“`typescript
interface Animal {
name: string;
eat(): void;
}
interface Dog extends Animal {
bark(): void;
}
interface Cat extends Animal {
meow(): void;
}
let a: Animal = {
name: “Animal”,
eat() {
console.log(“Eating”);
},
};
let d: Dog = {
name: “Dog”,
eat() {
console.log(“Eating dog food”);
},
bark() {
console.log(“Barking”);
},
};
let c: Cat = {
name: “Cat”,
eat() {
console.log(“Eating cat food”);
},
meow() {
console.log(“Meowing”);
},
};
a.eat(); // Output: Eating
d.eat(); // Output: Eating dog food
c.eat(); // Output: Eating cat food
“`
In this example, we declared three interfaces: `Animal`, `Dog`, and `Cat`. We overrode the `eat` method in the `Dog` and `Cat` interfaces to provide different implementations for the `eat` method. We also added a new method called `bark` to the `Dog` interface and a new method called `meow` to the `Cat` interface. We declared three objects of type `Animal`, `Dog`, and `Cat` and initialized them with the corresponding interface properties and methods. We can see that the `eat` method has been overridden for the `Dog` and `Cat` objects, and the new methods `bark` and `meow` are available for the `Dog` and `Cat` objects respectively.
FAQs
What is an interface in TypeScript?
An interface in TypeScript is a construct that defines the structure of an object.
What is the purpose of extending an interface in TypeScript?
The purpose of extending an interface in TypeScript is to create a new interface that inherits all the properties and methods of an existing interface and add additional properties or methods.
Is it possible to override properties and methods when extending an interface in TypeScript?
Yes, it is possible to override properties and methods when extending an interface in TypeScript.
Can an interface have more than one parent in TypeScript?
No, an interface can only have one parent in TypeScript.
Can we declare a type based on an extended interface in TypeScript?
Yes, we can declare a type based on an extended interface in TypeScript. We can use the `interface` keyword to declare the interface and the `type` keyword to declare the type.
Conclusion
In conclusion, extending interfaces in TypeScript is a powerful feature that allows developers to create new interfaces that inherit the properties and methods of existing interfaces. This feature can help to reduce the amount of code needed to declare objects and improve the development experience. Understanding how to extend interfaces in TypeScript can also help developers create more efficient and maintainable code.