Suggest an editImprove this articleRefine the answer for “What does the implements keyword do?”. Your changes go to moderation before they’re published.Approval requiredContentWhat you’re changing🇺🇸EN🇺🇦UAPreviewTitle (EN)Short answer (EN)The `implements` keyword tells TypeScript that a class **promises to implement** all the properties and methods described in an interface. **Key point:** if a class fails to implement something from the interface, TypeScript raises a compilation error.Shown above the full answer for quick recall.Answer (EN)Image## 1. What `implements` does > The `implements` keyword 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 ```javascript 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 ```javascript interface Person { name: string; greet(): void; } class User implements Person { name: string; } // Error: method greet() is missing ``` > TypeScript 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. ```javascript interface Flyable { fly(): void; } interface Swimmable { swim(): void; } class Duck implements Flyable, Swimmable { fly() { console.log("Flying"); } swim() { console.log("Swimming"); } } ``` > The `Duck` class 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` ```javascript 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, Flyable` implements the **contract**. > > `Eagle extends Bird` **inherits 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 `implements` keyword disappears; - there are no checks at runtime. ```javascript 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 the `ConsoleLogger` class itself. --- ## 6. You can implement interfaces with fields and methods ```javascript 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 `id` and `name` exist with the required types; - that a `getInfo()` method exists, returning `string`. --- ## 7. Implementing interfaces with `readonly` and optional fields ```javascript 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 } ``` > `readonly` keeps its value, > the class cannot redefine the `version` property after initialization. --- ## 8. You can implement an interface inherited from others ```javascript 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(); } ``` > `Admin` implements **all the properties**, > including those that came from `Entity` and `Timestamped`. --- ## 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: ```javascript 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 > `implements` in 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`: > > - `extends` **inherits implementation** (code, fields, methods); > - `implements` **guarantees implementation of a contract** (shape only).For the reviewerNote to the moderator (optional)Visible only to the moderator. Helps review go faster.