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

Object.values on an enum with template literal values #3329

Closed
Hacksore opened this issue Aug 21, 2023 · 3 comments
Closed

Object.values on an enum with template literal values #3329

Hacksore opened this issue Aug 21, 2023 · 3 comments

Comments

@Hacksore
Copy link

Hacksore commented Aug 21, 2023

Repro: https://github.com/Hacksore/test-ts-objects-literals

This only happens with esbuild and when you use template literal values.

I can't repro on tsplayground

repro code

const staticMember = "static";

enum TemplateLiteral {
  Minecraft = `minecraft_${staticMember}`,
  Lol = `lol_${staticMember}`,
}

console.log(Object.values(TemplateLiteral);

expected

[ 'minecraft_static', 'lol_static' ]

actual

[ 'minecraft_static', 'Minecraft', 'lol_static', 'Lol' ]
@evanw
Copy link
Owner

evanw commented Aug 24, 2023

TypeScript's rule for this is the backward mapping is only omitted if the initializer is a string literal, which is also the rule that esbuild implements. I think the issue here is that esbuild doesn't necessarily inline constants in the exact same situations that TypeScript does, which affects whether the initializer is a string or not.

The general solution here isn't for esbuild to replicate TypeScript's behavior exactly because that's impossible (TypeScript's behavior for this may involve type information, for example with the inlining of declare const, and esbuild deliberately doesn't emulate TypeScript's type system).

I think in this case esbuild could potentially consider a template literal initializer to be the same as a string literal. Then esbuild would mostly match TypeScript's behavior (except in the case where TypeScript can't inline the string, but then TypeScript emits a compile error so esbuild's behavior doesn't really matter in that case).

@Hacksore
Copy link
Author

Hacksore commented Sep 3, 2023

My workaround for is migrating back to ts-node instead of using tsx.

@evanw if you do fix this let me know but at least this issue is here for others who encounter the behaviour.

Closing for now.

@Hacksore Hacksore closed this as completed Sep 3, 2023
@evanw
Copy link
Owner

evanw commented Sep 13, 2023

I plan to apply the fix that I described above.

@evanw evanw reopened this Sep 13, 2023
@evanw evanw closed this as completed in 4c5db58 Sep 13, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants