David Miani David Miani - 1 month ago 6
TypeScript Question

How to generate correct ordering of files with typescript?

Consider two typescript files:

File

a.ts
:

module SomeModule {
export class AClass {
}
}


File
b.ts
:

module SomeModule {
export var aValue = new AClass();
}


If you compile them using
tsc -out out.js b.ts a.ts
, it compiles without error and
out.js
has the contents:

var SomeModule;
(function (SomeModule) {
SomeModule.aValue = new SomeModule.AClass();
})(SomeModule || (SomeModule = {}));
var SomeModule;
(function (SomeModule) {
var AClass = (function () {
function AClass() {
}
return AClass;
})();
SomeModule.AClass = AClass;
})(SomeModule || (SomeModule = {}));


This is a problem, as
SomeModule.AClass
is used before it is defined. Now the problem can be fixed by adding
///<reference path="a.ts" />
to the top of the
b.ts
file. This will ensure the
a.ts
is output before the
b.ts
file.

However, the project still compiles without warning without this line, so fixing this is a bit error prone. Worse, when used in a build script or using
gulp-typescript
, often the project will work fine initially without the line (due to the ordering being correct initially), but then start failing at random later on. So I'm looking for a way to ensure the file is in the correct order. Solutions I have thought of:


  1. Get typescript to determine the file order based on the implicit references to other files.

  2. Enforce references to other files, throwing an error if they are missing.



Unfortunately I couldn't get either to work. Is there a solution to this?

Answer

Unfortunately I couldn't get either to work. Is there a solution to this

Don't use --out https://github.com/TypeStrong/atom-typescript/blob/master/docs/out.md. Instead use external modules: https://www.youtube.com/watch?v=KDrWLMUY0R0

Comments