Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

154
Views
Typescript Factory class that avoids using a switch statement when using enum as an input?
abstract class Animal {
    abstract sound(): void;
}

class Cat extends Animal {
    sound(){
        console.log("meow");
    }
}

class Dog extends Animal {
    sound(){
        console.log("bark");
    }
}

enum AnimalType {
  Cat = 1,
  Dog = 2,
}

class AnimalFactory {
  static createAnimal(type: AnimalType): Animal {
    switch (type) {
      case AnimalType.Cat:
        return new Cat();
      case AnimalType.Dog:
        return new Dog();
      default:
        throw new Error("unsupported animal type");
    }
  }
}

const cat = AnimalFactory.createAnimal(AnimalType.Cat);
const dog = AnimalFactory.createAnimal(AnimalType.Dog);
cat.sound();
dog.sound();

Playground Link

I'm trying to get better at the Open–closed principle and I'm trying to figure out how to avoid using switch statements and decouple the factory class with the enum when I add more subclasses of Animal. The user via an API is sending over what animal is to be created via a simple enum. Is it possible to modify the AnimalFactory in a way that I don't need to modify the factory class every time I add another enum value and subclass of Animal?

Am I going at this wrong and I need more than an enum to use as an argument?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You can define a constant that contains a mapping of enum values to class types, and use a mapped type to create a type mapping:

abstract class Animal {
    abstract sound(): void;
}

class Cat extends Animal {
    sound() {
        console.log("meow");
    }
}

class Dog extends Animal {
    sound() {
        console.log("bark");
    }
}

enum AnimalType {
  Cat = 1,
  Dog = 2,
}

const animals = {
     [AnimalType.Cat]: Cat,
     [AnimalType.Dog]: Dog
};

type Animals = {
     [A in keyof typeof animals]: InstanceType<typeof animals[A]>
};

class AnimalFactory {
  static createAnimal<A extends keyof Animals>(type: A): Animals[A] {
    return new animals[type]();
  }
}

const cat = AnimalFactory.createAnimal(AnimalType.Cat);
const dog = AnimalFactory.createAnimal(AnimalType.Dog);
cat.sound();
dog.sound();

See it on the playground

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!