Skip to content

ES6 module immediately exported var clash with local "exports" variable #33043

@lkho

Description

@lkho

TypeScript Version: 3.5.1 (playground), 3.5.3

Search Terms:
es6 module exports object scope declare declaration overwrite

Code

export const a = 1
function b () {
    const exports = 2
    console.log(a)
}
b()

Expected behavior:
output from babel with es2015 preset
https://babeljs.io/repl#?babili=false&browsers=&build=&builtIns=false&spec=false&loose=false&code_lz=KYDwDg9gTgLgBAYwgOwM7wIZwLxwIwBQAZgK7IIwCWKcARnABQCUcA3gXJ4iunKJLFQ44AJg5ckaCABtgAOmkQA5gwxMCAXwK1mQA&debug=false&forceAllTransforms=false&shippedProposals=false&circleciRepo=&evaluate=false&fileSize=false&timeTravel=false&sourceType=module&lineWrap=false&presets=es2015&prettier=false&targets=&version=7.5.5&externalPlugins=

"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.a = void 0;
var a = 1;
exports.a = a;

function b() {
  var exports = 2;
  console.log(a);
}

b();

Actual behavior:
console.log is referencing the local exports.a instead of the global const a

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.a = 1;
function b() {
    var exports = 2;
    console.log(exports.a);
}
b();

However, if I split the export const into two, it will work.

const a = 1
export { a }

Playground Link:
http://www.typescriptlang.org/play/?target=1#code/KYDwDg9gTgLgBAYwgOwM7wIZwLxwIwCwAUAGYCuyCMAlinAEZwAUAlHAN7FzeIrpyhIsVDjgAmLjyRoIAG2AA6WRADmTDC2IBfYvVZA

Related Issues: can't find

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugA bug in TypeScriptDomain: ES ModulesThe issue relates to import/export style module behaviorHelp WantedYou can do this

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions