Classes Implement Interfaces
What
The implements clause allows a class to verify that it correctly follows a specific interface contract.
How
You list the interfaces after the class name. If the class is missing a property or method required by the interface, TypeScript will show an error:
interface Drivable {
drive(distance: number): void;
}
class Car implements Drivable {
drive(d: number) { console.log(`Driving ${d} miles`); }
}
Why
Using implements ensures that your classes remain compatible with the rest of your system’s types. It allows you to use class instances anywhere that specific interface is expected, facilitating polymorphic and modular code design.
Summary
implements is the bridge between the flexible world of interfaces and the structured world of classes.