KEMBAR78
Exclude enum/number compatibility rule from comparable relation by ahejlsberg · Pull Request #42472 · microsoft/TypeScript · GitHub
Skip to content

Conversation

@ahejlsberg
Copy link
Member

Fixes #42442.

@typescript-bot typescript-bot added Author: Team For Milestone Bug PRs that fix a bug with a specific milestone labels Jan 24, 2021
@ahejlsberg
Copy link
Member Author

@typescript-bot test this
@typescript-bot user test this
@typescript-bot run dt

@typescript-bot
Copy link
Collaborator

typescript-bot commented Jan 24, 2021

Heya @ahejlsberg, I've started to run the extended test suite on this PR at fd1a4cb. You can monitor the build here.

@typescript-bot
Copy link
Collaborator

typescript-bot commented Jan 24, 2021

Heya @ahejlsberg, I've started to run the parallelized Definitely Typed test suite on this PR at fd1a4cb. You can monitor the build here.

@typescript-bot
Copy link
Collaborator

typescript-bot commented Jan 24, 2021

Heya @ahejlsberg, I've started to run the parallelized community code test suite on this PR at fd1a4cb. You can monitor the build here.

@typescript-bot
Copy link
Collaborator

The user suite test run you requested has finished and failed. I've opened a PR with the baseline diff from master.

@ahejlsberg
Copy link
Member Author

The only test changes related to this PR are some new errors in the RWC vscode project. All but one of them are flagging real issues. For example, code that declares something as a KeyCode[], but then compares element values to -1, which doesn't match any declared KeyCode member. So, the array declaration should really be (KeyCode | -1)[].

The one undesired new error is related to this enum declaration in vs/editor/common/modes.ts:

/**
 * Open ended enum at runtime
 * @internal
 */
export const enum LanguageId {
	Null = 0,
	PlainText = 1
}

The intent here is for LanguageId to be a numeric (i.e. non-union) enum. This can be accomplished by changing one of the enum values to a computed expression:

export const enum LanguageId {
	Null = +0,
	PlainText = 1
}

Subtle, but actually an intended and documented behavior.

@DanielRosenwasser DanielRosenwasser added the Breaking Change Would introduce errors in existing code label Jan 25, 2021
@DanielRosenwasser
Copy link
Member

This seems like almost pure goodness, but I would still err on the side of saying that this can wait for 4.3. That +0 workaround is pretty subtle.

@DanielRosenwasser
Copy link
Member

What is the case for the non-union enum? It's unclear from that example.

@DanielRosenwasser
Copy link
Member

@ahejlsberg
Copy link
Member Author

What is the case for the non-union enum? It's unclear from that example.

The code that broke compares a value of type LanguageId to the numeric value 73. I guess the intent is to create symbolic names for a few special language IDs, but otherwise allow any integer value.

@ahejlsberg
Copy link
Member Author

Let me know if https://github.com/microsoft/TypeScript/wiki/Breaking-Changes#union-enums-cannot-be-compared-to-any-number accurately reflects the change here.

Yes, looks good.

@ahejlsberg ahejlsberg merged commit f918bd9 into master Mar 6, 2021
@ahejlsberg ahejlsberg deleted the fix42442 branch March 6, 2021 14:52
@microsoft microsoft locked as resolved and limited conversation to collaborators Oct 21, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

Author: Team Breaking Change Would introduce errors in existing code For Milestone Bug PRs that fix a bug with a specific milestone

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

Comparing numeric enum to number narrows variable to 'never'

3 participants