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

197
Views
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 answers
Answer question

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 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!