Skip to content

Compiler elides import of identifier that is referenced in emitted code.  #6003

@yortus

Description

@yortus
// file: foo.ts
import BluebirdPromise from 'bluebird';
export async function foo(): BluebirdPromise { }

// file: foo.js
function foo() {
    return __awaiter(this, void 0, BluebirdPromise, function* () { });
}
exports.foo = foo;

(Compiled with target=ES6, module=commonjs, noEmitHelpers=true).

The BluebirdPromise import has been elided in the generated code, resulting in "ReferenceError: BluebirdPromise is not defined" at runtime.

The elision occurs because BluebirdPromise only appears in a type position which up until v1.7 meant that the compiler could safely elide it. But the compiler now emits this type annotation into the JavaScript output so that elision assumption is no longer safe.

IMO the problem here is not the elision logic, its the fact that the compiler now emits code that depends on type annotaitons, so the type system no longer enjoys the quality of being fully erasable, which I understood to be a core design principle of the language. I thought the idea of using type annotations to change runtime behaviour would have been smacked down by @ahejlsberg and the team before it got into production.

Regardless of that contended side-topic, I believe this is a bug.

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugA bug in TypeScriptDuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions