Understanding Interfaces in TypeScript
TypeScript is a statically typed superset of JavaScript that adds several powerful features, one of which is Interfaces. Interfaces provide a way to define the shape of objects, ensuring that the objects adhere to a certain structure. They help in enforcing a contract between different parts of the application, allowing you to define properties, methods, and events that objects or classes must implement.
In this blog post, we will cover interfaces in TypeScript, how to declare them, use them with objects, and see their use with classes, inheritance, and arrays. We will also look at how interfaces provide a way to implement multiple inheritance in TypeScript, something that is otherwise not directly supported.
What is an Interface?
An interface in TypeScript is a proforma or a contract that defines the properties and methods an object or class should implement. Unlike classes, interfaces do not contain any implementation details. Instead, they provide a blueprint for objects or classes.
Key Points:
-
Interfaces only define the structure or type of an object.
-
Interfaces cannot contain any implementation code.
-
Interfaces can be implemented by classes to ensure that the class follows the structure defined in the interface.
Declaring an Interface
To declare an interface in TypeScript, we use the interface keyword followed by the interface name and the body (with the properties and methods that must be implemented).
Syntax:
Example:
Here, we declare an interface Employee to represent the structure of an employee object:
Using Interfaces with Objects
Once we define an interface, we can create objects that implement the interface. The object must match the structure defined in the interface. If any properties are missing or have incorrect types, TypeScript will raise an error.
Example:
In the above code:
-
The
Employeeinterface defines the structure that objects must adhere to. -
Both
salesandcustomerSupportobjects follow the structure defined in theEmployeeinterface.
Optional Parameters in Interfaces
You can also define optional properties in an interface. To make a property optional, you append a question mark (?) to the property name.
Example:
Explanation:
-
The
carproperty is optional in theEmployeeinterface. It can be provided or omitted when creating an object. -
The
salesobject doesn’t define acarproperty, while thecustomerSupportobject does.
Interfaces and Inheritance
Interfaces in TypeScript can be extended by other interfaces. This allows you to create a new interface that inherits properties and methods from one or more interfaces.
Example:
Simple Interface Inheritance
Explanation:
-
The
WebDesignStudentinterface extends theStudentinterface, so it inherits therollproperty. -
It also adds a new property
course.
Multiple Interface Inheritance
TypeScript supports multiple interface inheritance. You can extend more than one interface by separating the interfaces with commas. This allows you to combine properties from different interfaces into a single class or interface.
Example:
Explanation:
-
The
Childinterface extends bothIParent1andIParent2, inheriting propertiesv1andv2. -
The object
Iobjmust include bothv1andv2, as defined in theChildinterface.
Interfaces and Arrays
Interfaces in TypeScript are capable of defining both the type of keys and values. This allows you to create complex types like arrays with specific types.
Example:
Explanation:
-
The
studentsListinterface uses an index signature to specify that the index (number) will map to astringvalue. -
The
nameListarray is created using this interface, where the index is a number and the value is a string.
Using Interfaces with Classes
TypeScript allows classes to implement interfaces. When a class implements an interface, it is required to implement all the properties and methods defined by that interface.
Example:
Explanation:
-
The
Carinterface defines acolorproperty. -
The
Mercedezclass implements theCarinterface and provides its own implementation for thecolorproperty and adds a new propertycomputerized.
Interfaces and the instanceof Operator
You can use the instanceof operator to check if an object is an instance of a class that implements a certain interface. This is useful in certain situations, such as when working with multiple classes that implement the same interface.
Example:
Conclusion
Interfaces in TypeScript are a powerful way to define the structure of objects and classes. They allow you to create contracts that objects or classes must adhere to, ensuring that your code is consistent and follows the desired structure.
Key points covered in this blog post:
-
Declaring Interfaces: Use the
interfacekeyword to define a contract for objects or classes. -
Optional Parameters: Use
?to define optional properties in interfaces. -
Inheritance: Interfaces can extend other interfaces, allowing you to reuse and combine multiple interfaces.
-
Interfaces with Arrays: Interfaces can define the type of values and keys in arrays.
-
Interfaces and Classes: Classes can implement interfaces to ensure they follow a specific structure.
