Spring SALE

Adapter in TypeScript

Adapter is a structural design pattern, which allows incompatible objects to collaborate.

The Adapter acts as a wrapper between two objects. It catches calls for one object and transforms them to format and interface recognizable by the second object.



Usage examples: The Adapter pattern is pretty common in TypeScript code. It’s very often used in systems based on some legacy code. In such cases, Adapters make legacy code work with modern classes.

Identification: Adapter is recognizable by a constructor which takes an instance of a different abstract/interface type. When the adapter receives a call to any of its methods, it translates parameters to the appropriate format and then directs the call to one or several methods of the wrapped object.

Conceptual Example

This example illustrates the structure of the Adapter design pattern and focuses on the following questions:

  • What classes does it consist of?
  • What roles do these classes play?
  • In what way the elements of the pattern are related?

index.ts: Conceptual example

 * The Target defines the domain-specific interface used by the client code.
class Target {
    public request(): string {
        return 'Target: The default target\'s behavior.';

 * The Adaptee contains some useful behavior, but its interface is incompatible
 * with the existing client code. The Adaptee needs some adaptation before the
 * client code can use it.
class Adaptee {
    public specificRequest(): string {
        return '.eetpadA eht fo roivaheb laicepS';

 * The Adapter makes the Adaptee's interface compatible with the Target's
 * interface.
class Adapter extends Target {
    private adaptee: Adaptee;

    constructor(adaptee: Adaptee) {
        this.adaptee = adaptee;

    public request(): string {
        const result = this.adaptee.specificRequest().split('').reverse().join('');
        return `Adapter: (TRANSLATED) ${result}`;

 * The client code supports all classes that follow the Target interface.
function clientCode(target: Target) {

console.log('Client: I can work just fine with the Target objects:');
const target = new Target();


const adaptee = new Adaptee();
console.log('Client: The Adaptee class has a weird interface. See, I don\'t understand it:');
console.log(`Adaptee: ${adaptee.specificRequest()}`);


console.log('Client: But I can work with it via the Adapter:');
const adapter = new Adapter(adaptee);

Output.txt: Execution result

Client: I can work just fine with the Target objects:
Target: The default target's behavior.

Client: The Adaptee class has a weird interface. See, I don't understand it:
Adaptee: .eetpadA eht fo roivaheb laicepS

Client: But I can work with it via the Adapter:
Adapter: (TRANSLATED) Special behavior of the Adaptee.

Adapter in Other Languages

Adapter in C# Adapter in C++ Adapter in Go Adapter in Java Adapter in PHP Adapter in Python Adapter in Ruby Adapter in Rust Adapter in Swift