microsoft / microsoft/TypeScript

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

Open
#33,043 4 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

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

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

Reproduce the provided code in the TypeScript playground and compare its emitted JavaScript with the Babel output linked in the issue. Trace the handling of an immediately exported variable and the nested local exports binding; done means the generated console.log still references the module-level value rather than the local binding.

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.