How do I split my module across multiple files in Typescript with node.js

Actually you can (by now):

file: class1.ts:

export class Class1 {
  name: string;

  constructor(name: string){
      this.name = name;
  }
}

file: class2.ts:

export class Class2 {
    name: string;
}

consolidating module file: classes.ts:

export { Class1 } from "./class1";
export { Class2 } from "./class2";

consuming file:

import { Class1, Class2 } from "./classes";

let c1 = new Class1("Herbert");
let c2 = new Class2();

In this manner you can have one class (or interface) per file. In one consolidating module file (classes.ts) you then reference all entities that make up your "module".

Now you only have to reference (import) on single module file to access all of your classes. You still have a neat compartmentalization between files.

Hope this helps anyone still looking.


Multi-file external modules are not supported yet in TypeScript.

For module structuring recommendations, this page has a good rundown of the options. In particular, if you have exactly one class per file, you can use export = so that the second line of code in your example would simply be var vec = new vector();, which would be reasonably straightforward.

Tags:

Typescript