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

Throw error for TypeScript declare const enum #11410

Conversation

dosentmatter
Copy link
Contributor

Q                       A
Fixed Issues? Fixes #10785
Patch: Bug Fix? 👍
Major: Breaking Change?
Minor: New Feature?
Tests Added + Pass? 👍
Documentation PR Link
Any Dependency Changes?
License MIT

In #10785 @fiesh reported that declare const enum was transpiling as if it was declare enum. Since we don't support const enum, an error should be thrown whether it has declare or not.

Throw error on `const enum` even if it has `declare`.

Resolves: babel#10785
@nicolo-ribaudo
Copy link
Member

I'm not sure if this should be categorized as a breaking change (and thus go in Babel 8) or as a bugfix.

@existentialism existentialism added the PR: Bug Fix 🐛 A type of pull request used for our changelog categories label Apr 22, 2020
@dosentmatter
Copy link
Contributor Author

dosentmatter commented Apr 23, 2020

@nicolo-ribaudo, I guess it depends how you interpret the docs.

In docs, we say https://babeljs.io/docs/en/babel-plugin-transform-typescript#caveats

This plugin does not support const enums because those require type information to compile.

We don't specify whether const enums includes declare const enum.

I think const enum and declare const enum are the same thing anyway. declare makes it so no code is generated and it is only a hint to the compiler. It doesn't affect const enum since const enum already generates 0 code.

See:
https://stackoverflow.com/questions/35019987/what-does-declare-do-in-export-declare-class-actions
https://www.typescriptlang.org/docs/handbook/enums.html#ambient-enums

tsc has same output for both:
image


I think it's better to treat it as a bug. Because it seems we just accidentally put the two if (node.const) { and if (node.declare) { in the wrong order.

The current code transpiles declare const enum correctly because it just removes it.

But the problem is with accessors. It's transpiling enum access as if it were declare enum. If you look at #10785, you'll see that

declare const enum Enum {
  A = 1,
}
console.log(Enum.A)

transpiles to

console.log(Enum.A)

If we supported const enum, it should be

console.log(1)

Since we don't transpile the accessors correctly, I think it's better to treat this as a bug. I'm not sure if we could consider the current functionality "working". It doesn't match the TS spec and I don't think we documented it.

If someone did depend on declare const enum working, they can fix it by changing to declare enum, which is the more correct in TypeScript.

@nicolo-ribaudo nicolo-ribaudo merged commit 9b71651 into babel:master Apr 23, 2020
@github-actions github-actions bot added the outdated A closed issue/PR that is archived due to age. Recommended to make a new issue label Jul 24, 2020
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 24, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area: errors area: typescript outdated A closed issue/PR that is archived due to age. Recommended to make a new issue PR: Bug Fix 🐛 A type of pull request used for our changelog categories
Projects
None yet
Development

Successfully merging this pull request may close these issues.

String valued enums differ from tsc
3 participants