Implements in Classes in TypeScript

Опубликовано: 16 Июнь 2026
на канале: Learn Language Hub
5
0

In TypeScript, the implements keyword is used by a class to promise that it follows the structure defined by an interface.

It enforces contract-based design and compile-time safety.

1. Core idea

An interface defines what a class must have.
A class that implements an interface defines how it is done.

interface User {
name: string;
age: number;
}

class Person implements User {
name: string;
age: number;

constructor(name: string, age: number) {
this.name = name;
this.age = age;
}
}


If the class is missing anything, TypeScript throws an error.

2. What implements actually does

Implements checks only the shape, not the internal implementation details.

It enforces:

Required properties

Required methods

Correct types

It does not:

Add code

Generate JavaScript

Affect runtime behavior

3. Missing members cause errors
interface Animal {
name: string;
speak(): void;
}

class Dog implements Animal {
name: string;

speak() {
console.log("Woof");
}
}


This is valid.

But if something is missing:

class Dog implements Animal {
name: string;
}


TypeScript error:

Property speak is missing

4. Method signatures must match exactly
interface Logger {
log(message: string): void;
}

class ConsoleLogger implements Logger {
log(message: string): void {
console.log(message);
}
}


Wrong types cause errors:

log(message: number): void // Error

5. Access modifiers rule

When implementing an interface:

Interface members are assumed to be public

You cannot make them private or protected

interface Service {
start(): void;
}

class MyService implements Service {
private start() {} // Error
}


Must be public.

6. Implementing multiple interfaces

A class can implement more than one interface.

interface Flyable {
fly(): void;
}

interface Swimmable {
swim(): void;
}

class Duck implements Flyable, Swimmable {
fly() {
console.log("Flying");
}

swim() {
console.log("Swimming");
}
}


This supports multiple behavior contracts, unlike inheritance.

7. Implements vs extends
Keyword Purpose
implements Enforces structure defined by an interface
extends Inherits implementation from a class

Example:

class Animal {
move() {
console.log("Moving");
}
}

interface Pet {
name: string;
}

class Dog extends Animal implements Pet {
name: string;
}


You can:

Extend one class

Implement many interfaces

8. Implements with generic interfaces

Very common in real-world apps.

interface RepositoryT {
add(item: T): void;
getAll(): T[];
}

class UserRepository implements RepositoryT {
private users: User[] = [];

add(user: User) {
this.users.push(user);
}

getAll() {
return this.users;
}
}


This guarantees:

Correct data type usage

Consistent API

9. Implements does not enforce constructor

Interfaces do not define constructors.

interface User {
name: string;
}

class Person implements User {
constructor(public name: string) {}
}


Constructor signature is free.

10. When to use implements

Use implements when:

You want strict structure enforcement

You need interchangeable implementations

You want safer refactoring

You are building services, repositories, adapters

Avoid it when:

You only need inheritance

A simple type alias is enough

Mental model

Interface equals contract
Implements equals promise to follow the contract

Quick summary

Implements enforces interface structure

Checked at compile time only

Does not generate JavaScript

A class can implement multiple interfaces

Common in scalable and maintainable codebases