What does the implements keyword do?
1. What implements does
The
implementskeyword tells TypeScript: "This class promises to implement all the properties and methods described in the interface".
If a class does not implement something from the interface, TypeScript raises a compilation error.
Example
interface Person {
name: string;
greet(): void;
}
class User implements Person {
name: string;
constructor(name: string) {
this.name = name;
}
greet() {
console.log(`Hello, ${this.name}!`);
}
}Everything is correct:
class User implements the Person interface,
because it contains both name and greet().
2. What happens if a class does not implement everything
interface Person {
name: string;
greet(): void;
}
class User implements Person {
name: string;
}
// Error: method greet() is missingTypeScript requires the class to strictly match the interface, otherwise the build fails.
3. Implementing several interfaces
A class can implement several interfaces at once, listed with a comma.
interface Flyable {
fly(): void;
}
interface Swimmable {
swim(): void;
}
class Duck implements Flyable, Swimmable {
fly() {
console.log("Flying");
}
swim() {
console.log("Swimming");
}
}The
Duckclass is required to implement all methods from both interfaces.
4. Difference between implements and extends
| Keyword | Where it is used | What it does |
|---|---|---|
extends | between classes or interfaces | Inherits implementation or structure |
implements | in classes | Guarantees implementation of an interface (contract) |
Example: extends vs implements
interface Animal {
eat(): void;
}
interface Flyable {
fly(): void;
}
class Bird implements Animal, Flyable {
eat() {
console.log("Pecks grain");
}
fly() {
console.log("Flying");
}
}
class Eagle extends Bird {
hunt() {
console.log("Hunts");
}
}
Bird implements Animal, Flyableimplements the contract.
Eagle extends Birdinherits the implementation and adds a new one.
5. implements checks only typing, not behavior
Interfaces in TypeScript exist only at compile time. After compiling to JavaScript:
- the
implementskeyword disappears; - there are no checks at runtime.
interface Logger {
log(msg: string): void;
}
class ConsoleLogger implements Logger {
log(msg: string) {
console.log(msg);
}
}The resulting JavaScript code has no interface and no
implements, only theConsoleLoggerclass itself.
6. You can implement interfaces with fields and methods
interface UserData {
id: number;
name: string;
getInfo(): string;
}
class User implements UserData {
id: number;
name: string;
constructor(id: number, name: string) {
this.id = id;
this.name = name;
}
getInfo() {
return `${this.name} (#${this.id})`;
}
}TypeScript checks:
- that
idandnameexist with the required types; - that a
getInfo()method exists, returningstring.
7. Implementing interfaces with readonly and optional fields
interface Config {
readonly version: string;
debug?: boolean;
}
class App implements Config {
version = "1.0.0";
// debug does not have to be implemented, since it is optional
}
readonlykeeps its value, the class cannot redefine theversionproperty after initialization.
8. You can implement an interface inherited from others
interface Entity {
id: number;
}
interface Timestamped {
createdAt: Date;
}
interface User extends Entity, Timestamped {
name: string;
}
class Admin implements User {
id = 1;
name = "Tim";
createdAt = new Date();
}
Adminimplements all the properties, including those that came fromEntityandTimestamped.
9. You can use implements to check a type's shape
TypeScript lets you use an interface as a "contract" for a class, to guarantee type compatibility even without inheritance:
interface Serializable {
toJSON(): string;
}
class Order implements Serializable {
constructor(public id: number, public total: number) {}
toJSON() {
return JSON.stringify({ id: this.id, total: this.total });
}
}Summary
implementsin TypeScript is used in classes to force a class to implement an interface (or several).It:
- checks that the class contains all the fields and methods of the interface;
- adds nothing at runtime (it is purely a type check);
- can be applied to several interfaces;
- makes code more strict and predictable.
Difference from
extends:
extendsinherits implementation (code, fields, methods);implementsguarantees implementation of a contract (shape only).
Short Answer
Interview readyA concise answer to help you respond confidently on this topic during an interview.