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

typescript

Solution

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.

Problem

It seems that the information on how to actually structure code when writing Typescript is next to non-existent. I want to make a server in node. It has external dependencies like socket.io. The server will be too big to put it all in one file (as I imagine is the case most of the time) so I figured I'd split it up. I want to have each class in a separate file and I want to be able to use them in the whole project without needing to do something crazy like ``` import vector = require("vector.ts"); var vec = new vector.Vector(); ``` How do I do that? So far it seems that I'm fighting on two fronts. When I get tsc to actually compile, node complains on runtime, but when I modify the code so that node would work, it doesn't compile. I'd appreciate if someone could take the time to go through this step by step.

Original source