microsoft / microsoft/TypeScript

Temp variable re-usage breaks JS when building into single file

Open
#55,560 10 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Needs More Info
Dominant language
Go
Stars
111k
Forks
14.4k
Avg merge
1d 19h
Merged PRs (30d)
117

Description

🔎 Search Terms

static

🕗 Version & Regression Information
⏯ Playground Link

https://www.typescriptlang.org/play?#code/PTAEGMEMBdwCwHTQM4ChwBtLOaAwjPKAN6qqgUAOArgEYYCW4oy0MToATgKaUD2naAFFOnUAF5QACgCUALlC0+fDN0gA7CQD4S5Cl27RqnTQViI+6kZwEBJddG4nIGABIaAJqs6yA3HoBffz0aeg5WdmZLazsHJ3UXd3UvJwlpeUVlVQ1tXX0DIxNQaE5qbn8KILIqOkZmCOgODz5uZAAlbgArbnBoNOwAT3VmWVzSfJ5CzQAzF2RuQP8AshBQDT5oOCcAWihzJDRMbFwAQXUNrc4zInGasPq2RuYefkEAZT4AW0M4BnUAczSsgUShUak04h0twok2MmjOFyc1ws6g+302f3+SRSPhkFVAVRCtXCjw4ljRP0x2O8QIyoOyEKhehhhjhxVK5UW1VAoTqLFJzGarQ63V6-WQQxGMjGzIKbNmGHmXOWqFW4C+lAYqmKW3moAATKBplrWqA-tA+GsWJjtZZuKAAO4MTagABEKGYe3gBzW502Oy9iBQoG22z41GgADETcVWtAEJ1cKHPnwPNRted1PbQ2xOP9DKBWvqAAwARgA7K6gA

💻 Code
// catch.ts
class Catch {

   public static reportErr = (): boolean => {
    return Catch.onErrorInternalHandler();
  };

  public static onErrorInternalHandler = (): boolean => {
    return true;
  };

  public static doesReject = async () => {
    return false
  };
}

// another-catch.ts
class AnotherCatch {
   public static reportSomething = (): boolean => {
    return AnotherCatch.onSomethingHandler();
  };

  public static onSomethingHandler = (): boolean => {
    return true;
  };

  public static doesReject = async () => {
    return false
  };
}

// compile these 2 files into a single one with "tsc catch.ts another-catch.ts --outFile test.js --module none --target es2017"
🙁 Actual behavior

In compiled JS it converts to:

var _a;
class Catch {
}
_a = Catch;
Catch.reportErr = () => {
    return _a.onErrorInternalHandler();
};
Catch.onErrorInternalHandler = () => {
    return true;
};
Catch.doesReject = async () => {
    return false;
};
var _a;
class AnotherCatch {
}
_a = AnotherCatch;
AnotherCatch.reportSomething = () => {
    return _a.onSomethingHandler();
};
AnotherCatch.onSomethingHandler = () => {
    return true;
};
AnotherCatch.doesReject = async () => {
    return false;
};

Which leads to _a.onErrorInternalHandler is not a function error when trying to perform Catch.reportErr() later in the code.

🙂 Expected behavior

In 5.2.0-beta it compiled to

var _a;
class Catch {
}
_a = Catch;
Catch.reportErr = () => {
    return Catch.onErrorInternalHandler();
};

And it worked well.

Additional information about the issue

No response

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 catch.ts and another-catch.ts using tsc catch.ts another-catch.ts --outFile test.js --module none --target es2017, then inspect the generated test.js. Compare the emitted temporary-variable handling with the expected output and verify that both static methods work when the files are bundled into one output file.

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.