Interfaces in TypeScript
An interface in TypeScript is a contract that defines the structure of an object. It specifies the properties and methods that an object must have, without providing any implementation. Interfaces are used solely for type-checking purposes and are removed during the compilation process.
Features:
- Defining object shapes: Ensuring objects have specific properties and methods.
- Improving code readability: Making it clear what properties and methods an object should have.
- Enforcing consistency: Preventing errors by ensuring objects adhere to a specific structure.
Hereâs a simple example of how to use interfaces in TypeScript:
interface Car {
make: string;
model: string;
year: number;
}
const myCar: Car = {
make: "Toyota",
model: "Corolla",
year: 2022
};
console.log(myCar);
Output:
{
make: 'Toyota',
model: 'Corolla',
year: 2022
}
In this example:
- Car Interface: Defines the structure of the car object with make, model, and year as required properties.
- myCar Object: An object that adheres to the Car interface with the specified properties.
Interface in a Class
In TypeScript, an interface defines a contract for properties and methods. A class that implements an interface must provide concrete implementations, ensuring consistency and type safety.
Now let's undersatnd this with the help of example:
interface Employee {
name: string;
age: number;
position: string;
}
class Manager implements Employee {
name: string;
age: number;
position: string;
constructor(name: string, age: number, position: string) {
this.name = name;
this.age = age;
this.position = position;
}
}
const manager1 = new Manager("John Doe", 35, "Project Manager");
console.log(manager1);
Output:
Manager { name: 'John Doe', age: 35, position: 'Project Manager' }
In this example:
- The Employee interface defines the structure for an employee, requiring name, age, and position properties.
- The Manager class implements the Employee interface, ensuring it has the required properties.
Interface for an Object
An interface can be used to define the shape of an object, specifying the required properties and their types. This ensures objects follow a consistent structure and prevents type-related errors.
Now let's understand this with the help of example:
interface Product {
id: number;
name: string;
price: number;
}
const product: Product = {
id: 1,
name: "Laptop",
price: 1200
};
console.log(product);
Output:
{ id: 1, name: 'Laptop', price: 1200 }
In this example:
- The Product interface specifies that a product must have an id (number), name (string), and price (number).
- The product object follows the Product interface and includes all required properties.
Interface with Method Signatures
Interfaces can define method signatures to specify what methods a class or object must implement, including their parameters and return types.
Now let's understand this with the help of example:
interface Calculator {
add(a: number, b: number): number;
subtract(a: number, b: number): number;
}
class SimpleCalculator implements Calculator {
add(a: number, b: number): number {
return a + b;
}
subtract(a: number, b: number): number {
return a - b;
}
}
const calc = new SimpleCalculator();
console.log(calc.add(5, 3));
console.log(calc.subtract(9, 4));
Output:
8
5
In this example:
- The Calculator interface defines two methods add and subtract, both accepting two numbers and returning a number.
- The SimpleCalculator class implements the Calculator interface, providing logic for the add and subtract methods.