Member-only story
Nodejs: What is the Interface?
In Node.js, an interface is a TypeScript
construct that describes the shape of an object, specifying the names, types, and optional or required status of its properties and methods.
Interfaces do not generate any code at runtime but provide a way to define a contract that other objects must adhere to. This allows for type-checking
and better tooling
in code editors.
Interfaces are useful for defining APIs and object shapes that can be shared across multiple modules.
For example, let’s say you have a module that exports a function that expects an object with a specific set of properties:
export function printPersonDetails(person: {name: string, age: number}) {
console.log(`${person.name} is ${person.age} years old.`);
}
This works fine for a simple function, but what if you have multiple functions across different modules that expect the same properties? You would need to repeat the same property definitions for each function.
By using an interface, you can define the expected shape of the object once and reuse it across multiple functions and modules:
interface Person {
name: string;
age: number;
}
export function printPersonDetails(person: Person) {
console.log(`${person.name} is ${person.age} years old.`);
}
export function…