In TypeScript, an interface is a way to define a contract or a blueprint for an object. It specifies the properties and methods that an object must have, along with their types. Interfaces do not have any implementation; they only describe the structure. Here is a simple example of an interface:
interface Person {
name: string;
age: number;
}
let person: Person = {
name: "John",
age: 30
};
In this example, the Person
interface defines that an object of type Person
must have a name
property of type string
and an age
property of type number
.
As shown in the previous example, interfaces are commonly used to define the shape of objects. You can use an interface to ensure that an object has a specific set of properties with the correct types.
interface Point {
x: number;
y: number;
}
function printPoint(point: Point) {
console.log(`x: ${point.x}, y: ${point.y}`);
}
let myPoint: Point = { x: 10, y: 20 };
printPoint(myPoint);
Interfaces can also be used to define the shape of functions. You can specify the parameters and the return type of a function using an interface.
interface AddFunction {
(a: number, b: number): number;
}
let add: AddFunction = function(a, b) {
return a + b;
};
console.log(add(5, 3));
Interfaces can be used to enforce a class to implement certain properties and methods. A class can implement an interface by using the implements
keyword.
interface Animal {
name: string;
makeSound(): void;
}
class Dog implements Animal {
name: string;
constructor(name: string) {
this.name = name;
}
makeSound() {
console.log("Woof!");
}
}
let myDog = new Dog("Buddy");
myDog.makeSound();
You can make properties in an interface optional by adding a ?
after the property name. This allows an object to have or not have that property.
interface Book {
title: string;
author?: string;
}
let myBook: Book = { title: "The Great Gatsby" };
console.log(myBook);
You can mark properties in an interface as readonly using the readonly
keyword. Once a readonly property is assigned a value, it cannot be changed.
interface User {
readonly id: number;
name: string;
}
let user: User = { id: 1, name: "Alice" };
// user.id = 2; // This will cause a compilation error
Interfaces can extend other interfaces, allowing you to create new interfaces based on existing ones.
interface Shape {
color: string;
}
interface Square extends Shape {
sideLength: number;
}
let mySquare: Square = { color: "blue", sideLength: 10 };
Interfaces should be as simple as possible. Avoid creating overly complex interfaces with too many properties and methods. This makes the code easier to understand and maintain.
Interfaces are great for defining the public API of a module or a library. They provide a clear contract for other developers who want to use your code.
TypeScript interfaces are a powerful feature that allows developers to define contracts for types. They can be used for objects, functions, and classes, and offer features like optional properties, readonly properties, and interface extension. By following common practices and best practices, you can make your TypeScript code more robust, maintainable, and easier to understand.