Skip to content

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

Closed

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 Anders Hejlsberg (@ahejlsberg) and the team before it got into production.

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

Activity

  1. mhegazy commented on Dec 9, 2015

    @mhegazy
    Contributor

    this is duplicate of #5998, it has since been fixed in master. please try typescript@next and let us know if you are still running into other issues.

  2. locked and limited conversation to collaborators on Jun 19, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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