Skip to content

If exported variable is renamed, tsc will generate code with a syntax error #39022

@gdh1995

Description

@gdh1995

TypeScript Version: typescript@4.0.0-dev.20200611 (also 3.9.5)

Search Terms: exported variable renamed

Code

// a.ts
let foo = 1
export { foo as fooValue }
export const testFoo = (val: number) => {
  if (val <= ++foo) {
    return val
  }
  return foo
}

And then run this in a terminal:

npx tsc a.ts && cat a.js

Expected behavior:

The command should output:

"use strict";
exports.__esModule = true;
exports.testFoo = exports.fooValue = void 0;
var foo = 1;
exports.fooValue = foo;
exports.testFoo = function (val) {
    if (val <= (exports.fooValue = ++foo)) {
        return val;
    }
    return foo;
};

Actual behavior:

The actual output is as the below, and require('./a.js') in Node.JS throws a SyntaxError

"use strict";
exports.__esModule = true;
exports.testFoo = exports.fooValue = void 0;
var foo = 1;
exports.fooValue = foo;
exports.testFoo = function (val) {
    if (val <= exports.fooValue = ++foo) {
        return val;
    }
    return foo;
};

If I run require("./a.js") in node, then it outputs:

Thrown:
R:\working\2\a.js:7
    if (val <= exports.fooValue = ++foo) {
        ^^^^^^^^^^^^^^^^^^^^^^^

SyntaxError: Invalid left-hand side in assignment

Playground Link: https://www.staging-typescript.org/play?target=1&module=1#

Related Issues: None

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScriptFix AvailableA PR has been opened for this issueRescheduledThis issue was previously scheduled to an earlier milestone

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions