Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

export import generates no code (perhaps due optimization) #512

Closed
Evgenus opened this issue Aug 22, 2014 · 8 comments
Closed

export import generates no code (perhaps due optimization) #512

Evgenus opened this issue Aug 22, 2014 · 8 comments
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue

Comments

@Evgenus
Copy link

Evgenus commented Aug 22, 2014

TLDR: TypeScript does not generate JS code for exported variables when they are not used in current compilation scope, only the declarations (.d.ts)

Example


source files

// a.ts
import b = require("b");
b.c.say();
// b.ts
export import c = require("c");
// c.ts
export function say() {
    console.log("Hello World!");
}

Success scenario. We are using a.ts directly. tsc a.ts --module commonjs

generated files

// a.js
var b = require("b");
b.c.say();
// b.js
var c = require("c");
exports.c = c;
// c.js
function say() {
    console.log("Hello World!");
}
exports.say = say;

Failure scenario. Imagine if a.ts is placed into another project and uses b.ts as external dependency. In that case we need to compile b.ts with declarations for it. tsc b.ts --module commonjs --declaration

// c.js
function say() {
    console.log("Hello World!");
}
exports.say = say;
// c.d.ts
export declare function say(): void;
// b.d.ts
export import c = require("c");
// b.js   IT IS EMPTY!

First, this behavior is strongly inconsistent since generated declarations contains definitions for code that does not exist in generated JS.
Also, the fact that the JS code does not get generated at all is a blocker for our current use cases.
We need this fixed as soon as possible.

@ahejlsberg
Copy link
Member

This appears to be a bug in the old compiler. The new compiler generates the following b.js in both cases:

// b.js
exports.c = require("c");

@Evgenus
Copy link
Author

Evgenus commented Aug 22, 2014

What version have you mentioned? npm version is 1.0.1 and it has that bug.

@RyanCavanaugh
Copy link
Member

The compiler version in the master branch right now. It is not published in npm yet.

@nexussays
Copy link

I spent several hours tonight dealing with this problem and trying to track it down online, so +1 for getting it to prod ASAP.

Is there an ETA for public release of this version? I couldn't find anything on the roadmap or elsewhere.

@basarat
Copy link
Contributor

basarat commented Sep 10, 2014

@nexussays
Copy link

Are new versions cut from master, so this fix will be part of 1.1? There's no milestone on this issue and no associated changelog.

@mhegazy mhegazy added this to the TypeScript 1.1 milestone Sep 11, 2014
@mhegazy
Copy link
Contributor

mhegazy commented Sep 11, 2014

@nexussays, yes master would be the right place to grab the fix for this.

@nexussays
Copy link

Great. Can't wait for 1.1 :)

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue
Projects
None yet
Development

No branches or pull requests

6 participants