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):
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 */ }
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!";
}
}
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).