microsoft / microsoft/TypeScript

Uncaught compiler error: Block-scoped variable from import used before its declaration.

Open
#17,308 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Bug Domain: Binder
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

TypeScript Version: 2.4.0 / nightly (2.5.0-dev.201xxxxx)

Code

Input Typescript

namespace A {
    export namespace B {
        export class C {
            constructor()
            {
                console.log("Hello World");
            }
        }
    }
}

import myA_B = myA.B;
import myA = A;

new myA_B.C();

Output JavaScript from tsc

var A;
(function (A) {
    var B;
    (function (B) {
        var C = (function () {
            function C() {
                console.log("Hello World");
            }
            return C;
        }());
        B.C = C;
    })(B = A.B || (A.B = {}));
})(A || (A = {}));
var myA_B = myA.B;
var myA = A;
new myA_B.C();

Expected behavior:
Expected the typescript compiler error Block-scoped variable 'myA' used before its declaration.

Actual behavior:
Runtime error of Uncaught TypeError: Cannot read property 'B' of undefined

Comments
The typescript compiler will catch this if I use const or let keywords instead, so I expected the same error handling for import in this case.

My team prefers using import over const or let when we can for performance of not pulling in javascript code from external files when we are only referencing types.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the reported TypeScript 2.4 case with tsc and compare it with the const or let variant described in the issue. Trace how the compiler handles the import alias and its referenced namespace before declaration. Done means the reported code receives the expected compile-time diagnostic instead of producing JavaScript that fails at runtime.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, typescript
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.