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

Broken equality between enum and union of its values #46112

Open
nin-jin opened this issue Sep 28, 2021 · 4 comments
Open

Broken equality between enum and union of its values #46112

nin-jin opened this issue Sep 28, 2021 · 4 comments
Labels
Bug A bug in TypeScript
Milestone

Comments

@nin-jin
Copy link

nin-jin commented Sep 28, 2021

Bug Report

🕗 Version & Regression Information

💻 Code

type Equals< A , B > =
		(
			<X>()=> X extends A ? 1 : 2
		) extends (
			<X>()=> X extends B ? 1 : 2
		)
			? true
			: false

enum sex { male , female }

type sex1 = (typeof sex)[ keyof typeof sex ]
type sex2 = sex.male | sex.female

type xxx = Equals< sex1 , sex > // false :(
type yyy = Equals< sex2 , sex > // false :(
type zzz = Equals< sex1 , sex2 > // true :)

🙁 Actual behavior

false for xxx and yyy.

🙂 Expected behavior

true everywhere.

@MartinJohns
Copy link
Contributor

Playground link

@andrewbranch andrewbranch added the Bug A bug in TypeScript label Sep 30, 2021
@andrewbranch andrewbranch added this to the Backlog milestone Sep 30, 2021
@ahejlsberg
Copy link
Member

Had a chance to take a quick look at this one. The example cleverly exploits one of the few places we use isTypeIdenticalTo in the compiler--which so happens to be part of a check for conditional type compatibility where we require the extends types to be identical. As the example reveals, we don't consider a union enum type (such as sex in the example) to be identical to a manually constructed union of its constituents. I'm pretty sure no one really cares about this distinction, so I'm inclined to say this is working as intended.

@ahejlsberg
Copy link
Member

That said, I see now that this is a regression. Makes me more inclined to think we should fix it.

@nin-jin
Copy link
Author

nin-jin commented Oct 27, 2021

I'm using it in tests of parse wild json to more precise type.
Here I take all possible values of enum.
And here it fails type test.

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

No branches or pull requests

4 participants