Microsoft/TypeScript

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

Open

#33,043 opened on 2019年8月23日

GitHub で見る
 (4 comments) (2 reactions) (0 assignees)TypeScript (48,455 stars) (6,726 forks)batch import
BugDomain: ES ModulesHelp Wanted

説明

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

コントリビューターガイド

ES6 module immediately exported var clash with local "exports" variable · Microsoft/TypeScript#33043 | Good First Issue