Skip to content

Inconsistent enum type merging #32621

Closed
Closed

Description

TypeScript Version: typescript@3.6.0-dev.20190730 (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.

Activity

  1. jack-williams commented on Jul 30, 2019

    @jack-williams
    Collaborator

    This regressed between 3.0.1 -> 3.1.6.

    And to add some keywords for searching:

    enum subtype reduction
    getUnionType

  2. RyanCavanaugh commented on Jul 30, 2019

    @RyanCavanaugh
    Member

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

  3. jack-williams commented on Jul 30, 2019

    @jack-williams
    Collaborator

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

  4. RyanCavanaugh commented on Aug 7, 2019

    @RyanCavanaugh
    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Design LimitationConstraints of the existing architecture prevent this from being fixed

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions