Consider the scenario with two typescript files:
In File a.ts
:
module SomeModule {
export class AClass {
}
}
And in File b.ts
:
module SomeModule {
export var aValue = new AClass();
}
When compiling them using tsc -out out.js b.ts a.ts
, there are no errors and out.js
contains the following content:
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 = {}));
The issue arises as SomeModule.AClass
is used before its definition. This problem can be resolved by adding
///<reference path="a.ts" />
at the beginning of the b.ts
file, ensuring that a.ts
is compiled before b.ts
.
Even though the project compiles without any warnings without this line, fixing it can be error-prone. Moreover, when integrated into build scripts or using gulp-typescript
, the project may initially work correctly without the reference, but later on, random failures might occur. Thus, I am seeking a method to guarantee proper file ordering. Some solutions considered include:
- Having typescript determine file order based on implicit references.
- Enforcing references to other files and throwing errors if they are missing.
However, neither of these approaches yielded successful results. Are there any viable solutions to address this issue?