Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding internal/external modules and import/require Typescript 0.8.2

Tags:

typescript

There are numerous StackOverflow questions that touch on this subject, but either aren't quite the same as what I'm attempting, or are for previous versions of TypeScript.

I'm working on a rather large TypeScript project, and have a given module broken up across multiple files, not quite one per class.

In 0.8.0, this worked fine:

//* driver.ts *//
/// <reference path="express.d.ts"/>
/// <reference path="a.ts"/>
/// <reference path="b.ts"/>

.

//* a.ts *//
/// <reference path="driver.ts"/>
module m {
  import express = module("express");

  export class a {
    A: m.b;
    A2: express.ServerResponse;
  }
}

.

//* b.ts *//
/// <reference path="driver.ts"/>
module m {
  export class b {
      B: number;
  }
}

In 0.8.1, I had to change a.ts with the export import trick:

//* a.ts *//
/// <reference path="driver.ts"/>
module m {
  export import express = module("express");

  export class a {
    A: m.b;
    A2: express.ServerResponse;
  }
}

In 0.8.2, however, imports can no longer be within the module declaration, so a.ts has changed to:

//* a.ts *//
/// <reference path="driver.ts"/>
import express = module("express");
module m {

  export class a {
    A: m.b;
    A2: express.ServerResponse;
  }
}

which now gives an error because a.ts does not see the extension of the module in b.ts.

What I understand:

  • a.ts has become an external module, because of the import statement.
  • removing the import in a.ts allows a and b and my module to merge together fine.
  • changing the import to a require statement loses the type definitions in express.d.ts

What I don't understand:

  • Is there really no way for me to get around this without merging all of my module files together?

I apologize if this is answered elsewhere -- just link me there -- but none of the other similar issues seem to answer this definitively.

like image 773
Crwth Avatar asked Feb 12 '13 15:02

Crwth


1 Answers

This is what I make of your situation.

Your modules...

You need to name your file after your module, so a.ts, should actually be m.ts and should contain something like...

import express = module('express');

export class a {
    A: b;
    A2: express.ServerResponse;
}

export class b {
    B: number;
}

You shouldn't be using reference statements here.

When you are running code on nodejs, you can't really split your code across multiple files because the file itself is your module - when you import m = module('m'); it will look for m.js. What you can do is organise your files in a folder structure.

import x = module('m/x'); // m/x.js
import y = module('m/y'); // m/y.js
like image 68
Fenton Avatar answered Sep 23 '22 12:09

Fenton