sgrtho
sgrtho

Reputation: 218

How to force TypeScript exports to the end of the JS output?

I use a lot of export statements like the following, usually to cumulate a module's exports at the bottom of the file:

export { foo1 as bar1, foo2 as bar2, ... }

Lately I have learned that...

let foo : number = 0;
export { foo as bar }

...is not at all the same as...

let foo : number; foo = 0;
export { foo as bar }

...because the latter delivers undefined in exports.bar. This happens because the compiled JavaScript has the export statement of exports.bar = foo before the assignment. I find this hardly intuitive. I read over the TypeScript module pages, but I seem to miss the description of this behaviour. Is it there?

Is there a way to force the output's export statements to be at the bottom rather than right after declaration? Thanks.

Upvotes: 2

Views: 741

Answers (1)

Mattias Buelens
Mattias Buelens

Reputation: 20159

This sounds more like a bug in TypeScript. For future reference, TypeScript 1.8 compiles the following code:

let foo : number; foo = 0;
export { foo as bar }

into this JavaScript:

"use strict";
var foo;
exports.bar = foo;
foo = 0;

I tried it with TypeScript 2.0 beta, and it seems like that fixes it:

"use strict";
var foo;
exports.bar = foo;
exports.bar = foo = 0;

Upvotes: 2

Related Questions