1. Introduction
An interface describes the shape a value must have — the properties and methods it must expose — without providing any implementation. A class can promise to satisfy one or more interfaces using the implements keyword, and the compiler checks that the class actually provides every member the interface requires.
Cricket analogy: A team's selection criteria sheet lists required skills like bowling at 140kph and fielding at slip without specifying how a player developed them, and selectors check each recruit meets every listed requirement.
2. Syntax
interface InterfaceName {
propertyName: Type;
methodName(): ReturnType;
}
class ClassName implements InterfaceName {
propertyName: Type;
methodName(): ReturnType {
// must be implemented
}
}
// A class can implement multiple interfaces:
class Other implements InterfaceA, InterfaceB { /* ... */ }3. Explanation
implements is a compile-time-only contract check: the interface itself produces no JavaScript code and is completely erased after compilation. A class can implement multiple interfaces at once, separated by commas, which is not possible with extending a base class (TypeScript classes only support single inheritance via extends).
Cricket analogy: A player's all-rounder badge on the team sheet exists only for pre-match review and vanishes once play starts, and one player can be certified both a batting all-rounder and a fielding specialist at once, unlike inheriting a single mentor's exact technique.
This is the key distinction between implements and extends: extends inherits actual implementation (fields, method bodies, and behavior) from a single base class, while implements only enforces that the class's own code independently satisfies each interface member's type signature — nothing is inherited from the interface.
Cricket analogy: extends is like a young batter training directly under one senior mentor and inheriting his actual batting stance, while implements is like meeting a selector's checklist of required skills the player must independently develop.
A class implements an interface (a compile-time shape check, and you can implement several interfaces at once) versus extends a class (inherits real implementation, and TypeScript only allows extending one base class). A class can also do both at the same time: class Foo extends Base implements InterfaceA, InterfaceB.
4. Example
interface Flyable {
maxAltitude: number;
fly(): string;
}
interface Swimmable {
swim(): string;
}
class Duck implements Flyable, Swimmable {
maxAltitude = 300;
fly(): string {
return `Flying up to ${this.maxAltitude}m`;
}
swim(): string {
return "Paddling across the pond";
}
}
const duck = new Duck();
console.log(duck.fly());
console.log(duck.swim());5. Output
Flying up to 300m
Paddling across the pond6. Key Takeaways
- implements checks that a class provides every property/method an interface requires.
- A class can implement multiple interfaces at once, unlike extending only one base class.
- Interfaces produce no runtime JavaScript code; the check is compile-time only.
- implements does not inherit implementation, only enforces a type shape.
- A class can both extend a base class and implement one or more interfaces simultaneously.
Practice what you learned
1. What is the main difference between a class using 'implements' versus 'extends'?
2. How many interfaces can a single TypeScript class implement?
3. What happens to the Flyable and Swimmable interfaces after the Duck class is compiled to JavaScript?
4. What does duck.fly() return in the example, given maxAltitude = 300?
5. Can a class both extend a base class and implement interfaces at the same time?
Was this page helpful?
You May Also Like
Interfaces in TypeScript
Learn how TypeScript interfaces describe the shape of objects using structural typing, and why they are the primary tool for contract-based design.
Abstract Classes in TypeScript
Define shared structure and behavior for a family of subclasses using abstract classes and abstract methods that must be implemented by derived classes.
interface vs type in TypeScript
Compare TypeScript's `interface` and `type` keywords — when each is preferable, and what each can and cannot express.
Extending Interfaces in TypeScript
Learn how the `extends` keyword lets one interface inherit and build upon the members of one or more other interfaces.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics