Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

234
Visualizações
Switching on types in a factory in typescipt

Lets say I have a base class A and A1, A2, A3 all extends A. I create a factory class say, FactoryForA that implements a method say getObject as

getObject(typeToSwitch) {
  case A1: // return an  object of A1
  case A2: // return an object of A2
  case A3: // return an object of A3
}

I am looking for a way to make the argument typeToSwitch type safe.

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

My approach would be using infer typescript keyword to return the requested type. Something like the below:

class A {
    public getA = () => {}
}
class A1 extends A {
    constructor() { super(); }
    public getA = () => { return 'A1'; }
}
class A2 extends A {
    constructor() { super(); }
    public getA = () => { return 'A1'; }
}

// get the key map types ( a1 and a2 )
type Keys = keyof typeof FactoryA.AMap;

// get the classes types
type aTypes = typeof FactoryA.AMap[Keys];

// build the return type with generics
type ClassInstanceType<T> = T extends new () => infer R ? R : never;

class FactoryA {

    public static  AMap = { a1: A1, a2: A2 };

    public static getObject = (typeToSwitch: Keys  ): ClassInstanceType<aTypes> => {
        return new FactoryA.AMap[typeToSwitch]();
    }
}


const a1instance: A1 = FactoryA.getObject('a1');

console.log(a1instance.getA());
over 4 years ago · Santiago Trujillo Relatório

0

What I came up with is getObject<T extends new(...args: any[]) => A>(typeToSwitch: T).

It says T is something that extends A and is constructible, so it allows us to use a type as value(I am yet to figure this why). Also getObject(T extends A) does not work(why only a constructible type is supported.)

If anyone has suggestions or explanations please update the answer or comment.

Here is a link to an example of what I said above.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda