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

209
Visualizações
How can I extend multiple classes in typescript?

If I have two classes, is it possible to create a third class that extends both of them?

abstract class A {
    name
    age
    protected abstract print()
}
        
abstract class B {
    name
    age
}
        
// class C needs to inherit from both of the above classes

class C extends A , B {
}

   
   
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Yes, depending on the requirements there are several ways to do this. Given the classes A and B:

class A {
  constructor(public i: number){}
  foo() {
    return 123;
  }
}

class B {
  constructor(public name: string){}
  bar() {
    return "Hello from B!";
  }
}

you can do any of the following and have it just work (for some value of just-work):

  1. Inherit one class, implement the other as an interface (in TypeScript any class may be treated as an interface):

    class Cv1 extends B implements A { /* implement A-as-interface here */ }
    
  2. Create a type alias for A & B and implement the type alias (e. g. treat both classes as interfaces):

    type AandB = A & B;
    class Cv2 implements AandB {
      constructor(public i: number, public name: string) {}
      foo(...args: []) {
        return A.prototype.foo.apply(this, args);
      }
      bar() {
        return "Cv2 says GOODBYE!";
      }
    }
    
  3. Mix both classes together and inherit from the mixed class:

    // Exercise for the reader: do it without any
    function mixin(a: any, b: any): any {
      // Choice: Should isA apply to either mixin? Should it apply to neither?
      const aNotB = Object.defineProperties(Object.create(a.prototype), Object.getOwnPropertyDescriptors(b.prototype));
      const shadowClass: any = function shadowClass(){}
      shadowClass.prototype = aNotB;
      class mixinImpl extends shadowClass {}
      return mixinImpl;
    }
    
    class Cv3 extends mixin(A, B) { }
    

Caveat Emptor

There are limitations with this technique. For example, if B has a prototype chain of its own (e. g. B extends BPrime and BPrime extends BPrimordial, etc.) then none of the methods and properties of BPrime and BPrimordial will be copied over (you can walk the prototype chain yourself at class construction time to work around this if that's what you want to do). Also, instanceof will not work with any of the implements or .assign techniques (e. g. anInstanceOfC instanceof B will be false using any of the techniques above).

about 4 years ago · Juan Pablo Isaza 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