runtime.boot

Full Stack Systems
Production Mindset

CodeWithMihir

Engineering thoughtful products from interface to infrastructure.

CodeWithMihir

TypeScript Tutorial

TypeScript Implements Keyword Explained

Learn how the implements keyword works in TypeScript classes, including interface contracts, method requirements, and practical examples.

Welcome back! I am Mihir, and in this lesson we will learn the implements keyword in TypeScript.

implements lets a class promise that it follows an interface.


Basic implements Example

interface Logger {
  log(message: string): void;
}

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

ConsoleLogger must provide everything required by Logger.


Missing Members Cause Errors

interface Worker {
  work(): void;
}

class Developer implements Worker {}

This is invalid because Developer does not have a work method.

Correct:

class Developer implements Worker {
  work(): void {
    console.log("Writing code");
  }
}

implements with Properties

interface UserProfile {
  id: number;
  name: string;
}

class User implements UserProfile {
  constructor(public id: number, public name: string) {}
}

The constructor parameter properties satisfy the interface.


implements Multiple Interfaces

interface CanRead {
  read(): string;
}

interface CanWrite {
  write(value: string): void;
}

class FileManager implements CanRead, CanWrite {
  read(): string {
    return "content";
  }

  write(value: string): void {
    console.log(value);
  }
}

A class can implement more than one interface.


implements Does Not Copy Code

implements only checks the class shape.

It does not provide method bodies.

interface HasName {
  name: string;
}

class Person implements HasName {
  name = "Mihir";
}

The class still has to define the property itself.


implements vs extends

Use implements when a class follows an interface.

Use extends when a class inherits from another class.

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

class Dog extends Animal {}

extends gives behavior. implements checks a contract.


Quick Recap

  • implements makes a class follow an interface.
  • Missing required properties or methods cause errors.
  • A class can implement multiple interfaces.
  • implements does not copy code.
  • Use extends for inheritance and implements for contracts.

Next up, we will learn Abstract Classes →.