Skip to content

Compiler allows for using a value before it is imported #16166

@lambda-fairy

Description

@lambda-fairy

TypeScript Version: 2.3.4

Code

// foo.ts
console.log(BAR);
import {BAR} from './bar';
// bar.ts
export const BAR = 'bar';
// tsconfig.json
{
  "compilerOptions": {
    "target": "es5",
    "module": "commonjs",
    "strict": true
  }
}

Expected behavior:

TypeScript should reject foo.ts, because it is referring to BAR before it is initialized.

Actual behavior:

tsc raises no errors, and generates the following foo.js file:

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
console.log(bar_1.BAR);
var bar_1 = require("./bar");

This fails at runtime with a TypeError.

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugA bug in TypeScriptWon't FixThe severity and priority of this issue do not warrant the time or complexity needed to fix it

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions