Short answer: Typescript does not support splitting a class definition into multiple files.
Temporary solution . You can define an interface containing class members and two different classes that implement this interface. Then mix the properties from one class to another to create a combined class. For instance:
LargeClass.a.ts
interface LargeClass { methodA(): string; methodB(): string; } class LargeA implements LargeClass { methodA: () => string;
LargeClass.b.ts
class LargeB implements LargeClass { methodA() { return "Foo"; } methodB: () => string;
Usage.ts
// Using underscore extend to copy implementation from A to B var c:LargeClass = _.extend(new LargeA(), new LargeB()); // Manually mixing in a to b var a = new LargeA(); var b:LargeClass = new LargeB(); for (var prop in a) { b[prop]=a[prop]; }
This will not work if you need constructors for the class. And indeed this is suboptimal ... A workaround nonetheless :)
Oh, by the way, this works because Typescript does not highlight uniform property / field type declarations for classes - it only uses them for type checking.
I also understand that you can do this without interfaces and just make the class more beautiful ... I’ll leave how to do this as an exercise for readers for now ...
source share