Skip to content

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

Closed
@Evgenus

Description

@Evgenus

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugA bug in TypeScriptFixedA PR has been merged for this issue

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions