Skip to content

Implement erasable Enum Annotations#61414

Closed
caitp wants to merge 14 commits intomicrosoft:mainfrom
caitp:AsEnumAlternate2
Closed

Implement erasable Enum Annotations#61414
caitp wants to merge 14 commits intomicrosoft:mainfrom
caitp:AsEnumAlternate2

Conversation

@caitp
Copy link
Copy Markdown
Contributor

@caitp caitp commented Mar 13, 2025

In order to meet the goal of allowing "erasable" syntax only, which can be trivially converted to JS by removing type annotations, as described in #60790, including input from the TypeScript team to prefer the annotation to the "as enum" syntax.

This permits emitting code by simply erasing the Type Annotation. The TypeChecker sees references to the variable as a reference to an Enum-like type.

There are currently a few remaining obvious issues, for instance comments surrounding EnumMembers in the source are omitted from the emitted JS.

CC @nicolo-ribaudo @romulocintra

@github-project-automation github-project-automation bot moved this to Not started in PR Backlog Mar 13, 2025
@typescript-bot typescript-bot added the For Uncommitted Bug PR for untriaged, rejected, closed or missing bug label Mar 13, 2025
@typescript-bot
Copy link
Copy Markdown
Collaborator

This PR doesn't have any linked issues. Please open an issue that references this PR. From there we can discuss and prioritise.

@typescript-bot
Copy link
Copy Markdown
Collaborator

Looks like you're introducing a change to the public API surface area. If this includes breaking changes, please document them on our wiki's API Breaking Changes page.

Also, please make sure @DanielRosenwasser and @RyanCavanaugh are aware of the changes, just as a heads up.

}
}
else if (isEnumDeclaration(node)) {
else if (isEnumDeclaration(node) || (isVariableDeclaration(node) && node.initializer && isEnumLiteralExpression(node.initializer))) {
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here too

Comment on lines +14 to +17
const nonexist: E1 = E1.NonexistingShorthand; // ok
const exist: E1 = E1.ExistingShorthand; // ok
const ival: E1 = E1.Int; // ok
const sval: E1 = E1.String; // ok
Copy link
Copy Markdown

@demurgos demurgos Mar 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this implementation support types for individual variants? In other words, would the following work?

const exist: E1.ExistingShorthand = E1.ExistingShorthand; // ok
const ival: E1.Int = E1.Int; // ok
const sval: E1.String = E1.String;

I looked through the PR, but did not see code handling it or tests for this. It seems however to delegate to the existing enum handling in some places so I assume that it should work. I would appreciate a confirmation, thank you.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've made an effort to add a few of these tests (and they seem to be working, although it is a bit loose with accepting a wider number type for a unique numeric enum value. I believe that's normal for TS enums, though), but mostly I was focused on getting the invariant checks working. Thanks for the suggestion.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you 🙂

@caitp caitp force-pushed the AsEnumAlternate2 branch from 88a3dad to b06ff2d Compare March 18, 2025 15:59
@typescript-bot
Copy link
Copy Markdown
Collaborator

With 6.0 out as the final release vehicle for this codebase, we're closing all PRs that don't fit the merge criteria for post-6.0 patches. If you think this was a mistake and this PR fits the post-6.0 patch criteria, please post to the 6.0 iteration issue with details (specifically, which PR and which patch criteria it satisfies).

Next steps for PRs:

  • For crash bugfixes or language service improvements, PRs are currently accepted at the typescript-go repo
  • Changes to type system behavior should wait until after 7.0, at which point mainline TypeScript development will resume in this repository with the Go codebase
  • Library file updates (lib.d.ts etc) continue to live in this repo or the DOM Generator repo as appropriate

@github-project-automation github-project-automation bot moved this from Not started to Done in PR Backlog Mar 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

For Uncommitted Bug PR for untriaged, rejected, closed or missing bug

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

4 participants