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

Enum members aren't subtype reduced in unions #35867

Closed
DanielRosenwasser opened this issue Dec 27, 2019 · 5 comments · Fixed by #49675
Closed

Enum members aren't subtype reduced in unions #35867

DanielRosenwasser opened this issue Dec 27, 2019 · 5 comments · Fixed by #49675
Labels
Bug A bug in TypeScript Good First Issue Well scoped, documented and has the green light Has Repro This issue has compiler-backed repros: https://aka.ms/ts-repros Help Wanted You can do this Infrastructure Issue relates to TypeScript team infrastructure
Milestone

Comments

@DanielRosenwasser
Copy link
Member

DanielRosenwasser commented Dec 27, 2019

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;
}
@jack-williams
Copy link
Collaborator

Related: #32621?

@RyanCavanaugh RyanCavanaugh added the Bug A bug in TypeScript label Jan 13, 2020
@RyanCavanaugh RyanCavanaugh added this to the Backlog milestone Jan 13, 2020
@DanielRosenwasser
Copy link
Member Author

enum E {
    A,
    B,
    C,
}

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

Workbench Repro

@DanielRosenwasser
Copy link
Member Author

DanielRosenwasser commented Apr 12, 2022

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

@DanielRosenwasser DanielRosenwasser added Help Wanted You can do this Good First Issue Well scoped, documented and has the green light Infrastructure Issue relates to TypeScript team infrastructure labels Apr 12, 2022
@typescript-bot
Copy link
Collaborator

typescript-bot commented Apr 13, 2022

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

@typescript-bot
Copy link
Collaborator

typescript-bot commented Apr 14, 2022

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

lawrencecraft added a commit to lawrencecraft/TypeScript that referenced this issue Jun 16, 2022
lawrencecraft added a commit to lawrencecraft/TypeScript that referenced this issue Jun 16, 2022
acutmore pushed a commit to bloomberg/TypeScript that referenced this issue Jun 16, 2022
#68)

* Add case to capture enum subtype reduction (addresses microsoft#35867)

Signed-off-by: Lawrence Craft <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Good First Issue Well scoped, documented and has the green light Has Repro This issue has compiler-backed repros: https://aka.ms/ts-repros Help Wanted You can do this Infrastructure Issue relates to TypeScript team infrastructure
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants