Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

typescript

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.

like image 586
Luka Horvat Avatar asked Feb 11 '14 15:02

Luka Horvat


People also ask

Can TypeScript use CommonJS?

Yes, you can use it in a same manner that you would use it in Javascript. Typescript is superset of Javascript, all things possible in Javascript are also possible in Typescript.

How do I create a partial class in TypeScript?

Partial classes for TypeScript Unfortunately, TypeScript doesn't support partial classes. And according to this thread, it will never support it.

Why do you need separate modules in node JS?

Each module in Node. js has its own context, so it cannot interfere with other modules or pollute global scope. Also, each module can be placed in a separate . js file under a separate folder.


3 Answers

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.

like image 168
Wolfgang Avatar answered Oct 23 '22 20:10

Wolfgang


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.

like image 21
Ryan Cavanaugh Avatar answered Oct 23 '22 22:10

Ryan Cavanaugh


I was also trying to split a large module into smaller files, but the solution in this thread didn't isolate the import from each file, so I came up with another solution where I'm forced to import from the same file!

File A (filea.ts):

function funcA() { /* do something */ }
function funcB() { /* do something */ }

export default { funcA, funcB };

File B (fileb.ts):

function funcC() { /* do something */ }
function funcD() { /* do something */ }

export default { funcC, funcD };

Main Module (mainmodule.ts):

import filea from './filea';
import fileb from './fileb';

const mainmodule = { ...filea, ...fileb };

export = mainmodule;

This will result in this import:

import { funcA, funcD } from './mainmodule'
like image 1
Gabriel Pupim Avatar answered Oct 23 '22 22:10

Gabriel Pupim