Skip to content

Enum members aren't subtype reduced in unions #35867

Closed

Description

enum E {
    A,
    B,
    C,
}

function f(x: E | undefined) {
    return x ?? E.C;
}

Expected f to have type E, but instead it has type E | E.C

Playground

Doesn't have anything to do with ?? - can reproduce it with ||.

enum E {
    A = 1,
    B,
    C,
}

function f(x: E | undefined) {
    return x || E.C;
}

Activity

  1. jack-williams commented on Dec 27, 2019

    @jack-williams
    Collaborator

    Related: #32621?

  2. DanielRosenwasser commented on Jul 16, 2020

    @DanielRosenwasser
    MemberAuthor
    enum E {
        A,
        B,
        C,
    }
    
    function f(x: E | undefined) {
    //       ^?
        return x ?? E.C;
    }

    Workbench Repro

  3. DanielRosenwasser commented on Apr 12, 2022

    @DanielRosenwasser
    MemberAuthor

    I think this is fixed now. Would we have any volunteers willing to add a test case to prevent regressions?

  4. typescript-bot commented on Apr 13, 2022

    @typescript-bot
    Contributor

    👋 Hi, I'm the Repro bot. I can help narrow down and track compiler bugs across releases! This comment reflects the current state of the repro in the issue body running against the nightly TypeScript.


    Issue body code block by Daniel Rosenwasser (@DanielRosenwasser)

    ‼️ Exception: Error - Did not find a source file for /home/runner/work/TypeScript/TypeScript/index.ts

    Error: Did not find a source file for /home/runner/work/TypeScript/TypeScript/index.ts
        at Object.updateFile (/home/runner/work/_actions/microsoft/TypeScript-Twoslash-Repro-Action/master/dist/index.js:6876:15)
        at _loop3 (/home/runner/work/_actions/microsoft/TypeScript-Twoslash-Repro-Action/master/dist/index.js:1738:9)
        at twoslasher (/home/runner/work/_actions/microsoft/TypeScript-Twoslash-Repro-Action/master/dist/index.js:1744:17)
        at /home/runner/work/_actions/microsoft/TypeScript-Twoslash-Repro-Action/master/dist/index.js:777:44
        at runTwoslashRequests (/home/runner/work/_actions/microsoft/TypeScript-Twoslash-Repro-Action/master/dist/index.js:744:56)
        at run (/home/runner/work/_actions/microsoft/TypeScript-Twoslash-Repro-Action/master/dist/index.js:2169:75)
        at runMicrotasks (<anonymous>)
        at processTicksAndRejections (node:internal/process/task_queues:96:5)
    

    Historical Information
    Version Reproduction Outputs Time
    4.2.2, 4.3.2, 4.4.2, 4.5.2, 4.6.2

    👍 Compiled

    ⚠️ Way slower
  5. typescript-bot commented on Apr 14, 2022

    @typescript-bot
    Contributor

    👋 Hi, I'm the Repro bot. I can help narrow down and track compiler bugs across releases! This comment reflects the current state of this repro running against the nightly TypeScript.


    Comment by Daniel Rosenwasser (@DanielRosenwasser)

    ‼️ Exception: TypeError - Cannot read properties of undefined (reading 'getPositionOfLineAndCharacter')

    TypeError: Cannot read properties of undefined (reading 'getPositionOfLineAndCharacter')
        at Object.getPositionOfLineAndCharacter (/home/runner/work/_actions/microsoft/TypeScript-Twoslash-Repro-Action/master/dist/ts/nightly/lib/typescript.js:10384:27)
        at /home/runner/work/_actions/microsoft/TypeScript-Twoslash-Repro-Action/master/dist/index.js:1679:25
        at Array.map (<anonymous>)
        at _loop3 (/home/runner/work/_actions/microsoft/TypeScript-Twoslash-Repro-Action/master/dist/index.js:1677:40)
        at twoslasher (/home/runner/work/_actions/microsoft/TypeScript-Twoslash-Repro-Action/master/dist/index.js:1744:17)
        at /home/runner/work/_actions/microsoft/TypeScript-Twoslash-Repro-Action/master/dist/index.js:777:44
        at runTwoslashRequests (/home/runner/work/_actions/microsoft/TypeScript-Twoslash-Repro-Action/master/dist/index.js:744:56)
        at run (/home/runner/work/_actions/microsoft/TypeScript-Twoslash-Repro-Action/master/dist/index.js:2169:75)
        at runMicrotasks (<anonymous>)
        at processTicksAndRejections (node:internal/process/task_queues:96:5)
    

    Historical Information
    Version Reproduction Outputs Time
    4.2.2, 4.3.2, 4.4.2, 4.5.2, 4.6.2

    ⚠️ Assertions:

    • function f(x: E | undefined): E

    ⚠️ Way slower
  6. locked as resolved and limited conversation to collaborators on Oct 21, 2025
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

    BugA bug in TypeScriptGood First IssueWell scoped, documented and has the green lightHas ReproThis issue has compiler-backed repros: https://aka.ms/ts-reprosHelp WantedYou can do thisInfrastructureIssue relates to TypeScript team infrastructure

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions