Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove unused transpiled Typescript enum #1177

Open
mischnic opened this issue Apr 13, 2022 · 6 comments
Open

Remove unused transpiled Typescript enum #1177

mischnic opened this issue Apr 13, 2022 · 6 comments

Comments

@mischnic
Copy link
Contributor

Bug report or Feature request?

sub-optimal output

Version (complete output of terser -V or specific git commit)

5.12.1

Complete CLI command or minify() options used

terser -mc --module --toplevel index.js

terser input

// Typescript input to Babel
// enum Rec {
//   A = 123123,
//   B = 123124,
//   C = 123125,
//   D = 123126,
// }


var Rec;

(function (Rec) {
  Rec[Rec["A"] = 123123] = "A";
  Rec[Rec["B"] = 123124] = "B";
  Rec[Rec["C"] = 123125] = "C";
  Rec[Rec["D"] = 123126] = "D";
})(Rec || (Rec = {}));

console.log(123)

terser output or error

var o;!function(o){o[o.A=123123]="A",o[o.B=123124]="B",o[o.C=123125]="C",o[o.D=123126]="D"}(o||(o={})),console.log(123);

Expected result

Is there any change this could be removed (because Rec is unused)? This would have a rather high impact because it applies to all Typescript enums.

@mischnic
Copy link
Contributor Author

One solution would of course be to fix TSC/Babel/swc to instead do something using /*@__PURE__*/

var Rec = /*@__PURE__*/ (function (Rec) {
  Rec[Rec["A"] = 123123] = "A";
  Rec[Rec["B"] = 123124] = "B";
  Rec[Rec["C"] = 123125] = "C";
  Rec[Rec["D"] = 123126] = "D";
  return Rec;
})({});

but I imagine this Rec || (Rec = {}) expression is there for a reason and changing all of the tools will be very difficult. And even after that, there's still transpiled code like this published on npm.

@evanw
Copy link

evanw commented Apr 21, 2022

but I imagine this Rec || (Rec = {}) expression is there for a reason

That's for enum declaration merging. FWIW that's easy to solve for too: microsoft/TypeScript#27604 (comment). You just need to change ({}) into (Rec || {}) in your second comment and it should be exactly equivalent to TypeScript's current output (except easier to tree-shake). I've thought about this before because I actually changed esbuild's output to do this, and it has been working well.

@mischnic
Copy link
Contributor Author

@evanw Great to hear that this is working without problems in esbuild! I might try to change this for swc and Babel.

Related Babel issue: babel/babel#9268
swc issue: swc-project/swc#4453

@fabiosantoscode
Copy link
Collaborator

This is a construct that I think is too tricky to address in general, but Terser already supports another TS pattern of using the identity function to avoid repeating as unknown as OtherType.

So this specific usage can be addressed specifically IMO.

@ehoogeveen-medweb
Copy link

Looks like this will be fixed on the babel side: babel/babel#15467

@fabiosantoscode
Copy link
Collaborator

Lovely!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants