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

Inconsistent enum type merging #32621

Closed
zenorbi opened this issue Jul 30, 2019 · 4 comments
Closed

Inconsistent enum type merging #32621

zenorbi opened this issue Jul 30, 2019 · 4 comments
Labels
Design Limitation Constraints of the existing architecture prevent this from being fixed

Comments

@zenorbi
Copy link

zenorbi commented Jul 30, 2019

TypeScript Version: [email protected] (same with 3.2.4, 3.5.2, 3.5.3)

Search Terms: enum hover ternary union merging
enum subtype reduction
getUnionType

Code

enum SomeEnum {
  APPLE = 'APPLE',
  ORANGE = 'ORANGE',
  BANANA = 'BANANA'
}

function test(inputEnumMember: SomeEnum) {
  // Correctly gives `const a: SomeEnum`
  const a: SomeEnum.BANANA | SomeEnum = inputEnumMember;

  // Incorrectly gives `const b: SomeEnum.APPLE | SomeEnum`
  const b = Math.random() < 0.5 ? SomeEnum.APPLE : inputEnumMember;

  // Incorrectly gives `const c: SomeEnum.APPLE | SomeEnum` (really weird)
  const c = Math.random() < 0.5 ? SomeEnum.ORANGE : inputEnumMember;

  // Incorrectly gives `const d: SomeEnum.BANANA | SomeEnum`
  const d = Math.random() < 0.5 ? SomeEnum.BANANA : inputEnumMember;
}

Expected behavior:
All declared variables have the type SomeEnum or at least are consistently "wrong" (see const c as showing APPLE)

Playground Link:
https://www.typescriptlang.org/play/#code/KYOwrgtgBAyg9hYBRc0DeAoKUCCAFPAGSSgF4oByfIpCgGiygHkAlHAOQHETyLWPu9RgCEOYspVHsxFDAF8MGAGZgQAYwAuASzggoG4AGcNACi0gADmA0pIAWWAQARsABOALlgJkqAJRRMbAB6IKgAYThXV2BNABsATygAcy0ANyMoAAM1XWMoAENPeERbCEzGHJA8wq8S1AA6KXEAH1qfSAlzKxtUB2c3AG5FYNCASXVI6LjElPTDLMq8pyLvUvrqYihW4vayityNKCcJO3yNAAt613yQABMEE38AHigABnqAVigAfja1jZIni61lKfRcriGjBCUHGOSiMQ0CWSaQy2QOUDUKzqkHWBE221WqEyUBMAHdgFpXLdfPsqoc1CczpdrncHs83p8fn8GvwuICoMCevZHODISMYRN4dNkXMFujbljdo0xNIttzIOVsItDrdGRcrjd7hBHlAXu8vr8dmsmqqgZYQb0RYN5BggA

This is a really minor issue as it only applies to hover type information and the reported value while being weird isn't breaking anything.

@jack-williams
Copy link
Collaborator

jack-williams commented Jul 30, 2019

This regressed between 3.0.1 -> 3.1.6.

And to add some keywords for searching:

enum subtype reduction
getUnionType

@RyanCavanaugh
Copy link
Member

Does this have observable effects other than quick info? I don't think it does?

@jack-williams
Copy link
Collaborator

I guess this should only have observable effects up to the extent that SomeEnum and SomeEnum.APPLE | SomeEnum.ORANGE | SomeEnum.BANANA are distinguishable.

@RyanCavanaugh RyanCavanaugh added the Design Limitation Constraints of the existing architecture prevent this from being fixed label Aug 7, 2019
@RyanCavanaugh
Copy link
Member

Exactly when to perform subtype reduction is tricky business; there are some compromises we have to make in order for other scenarios to work. "Consistent" type display between different ways of generating observably-same types isn't something we can guarantee.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Design Limitation Constraints of the existing architecture prevent this from being fixed
Projects
None yet
Development

No branches or pull requests

3 participants