microsoft / microsoft/TypeScript

Assignment to constant variables imported from ES6 modules allowed

Open
#36,843 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

TypeScript Version:
3.7.5

Search Terms:

  • Assignment to constant variable in imported modules

Code

// originalFunction.js
let originalFunction = () => console.log('Original function executed! 🏁');
export {originalFunction};

// patchFunction.js
import {originalFunction} from "./originalFunction.js";
originalFunction = () => console.log('patched function 💥');

// run.js
import './patchFunction.js';
import {originalFunction} from './originalFunction.js';

originalFunction();

Expected behavior:
After tsc (with "allowJs": true, "target": "es5" or es6, "module": "commonjs") and running node run.js in output folder you should get Uncaught TypeError: Assignment to constant variable. at patchFunction.js:6

Just like you'd get with pure es6 in browser with this html (on same 3 original .js files):

<html >
<head></head>
<body>
<script type="module" src="originalFunction.js"></script>
<script type="module" src="patchFunction.js"></script>
<script type="module" src="run.js"></script>
</body>
</html>

Actual behavior:

Output is patched function 💥 because the code is transpiled in this manner:

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var originalFunction_js_1 = require("./originalFunction.js");
originalFunction_js_1.originalFunction = function () {
    console.log('patched function 💥');
};

Playground Link:

Hard to get it on playground because it requires having separate files on filesystem and actually loading them by browser.

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 issue with the three files originalFunction.js, patchFunction.js, and run.js using tsc with allowJs enabled, target es5 or es6, and module commonjs. Compare the emitted patchFunction.js with the shown output and run it under Node; done means assignment to the imported binding throws the expected TypeError instead of replacing the function.

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
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.