Skip to content

Type union collapses into the general type in condition #55158

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

Closed
FlandreDaisuki opened this issue Jul 26, 2023 · 4 comments
Closed

Type union collapses into the general type in condition #55158

FlandreDaisuki opened this issue Jul 26, 2023 · 4 comments

Comments

@FlandreDaisuki
Copy link

Bug Report

πŸ”Ž Search Terms

type union collapses

πŸ•— Version & Regression Information

  • I've tried from 3.3.3 to 5.2.0-beta and the result are the same.

⏯ Playground Link

Playground link

πŸ’» Code

const a = (Math.random() > 0.5)
  ? Object.freeze({ok: false, hello: 'world'})
  : Object.freeze({ok: false});

πŸ™ Actual behavior

type A = typeof a;
//   ^? shows type A = {readonly ok: false}

πŸ™‚ Expected behavior

type A = typeof a;
//   ^? shows type A = Readonly<{ok: false}> | Readonly<{ok: false, hello: 'world'}>
@MartinJohns
Copy link
Contributor

MartinJohns commented Jul 26, 2023

This is working as intended and happens because of type reduction. See #50171 and many many others.

Also: https://www.typescriptlang.org/docs/handbook/type-inference.html#best-common-type

@FlandreDaisuki
Copy link
Author

@MartinJohns thanks your help and reference links.

@Jack-Works
Copy link
Contributor

but I don't think this reduction is meaningful to the developers. it lost information that should be there.

@fatcerberus
Copy link

fatcerberus commented Jul 26, 2023

@Jack-Works Variations of this same issue have been raised many, many times in the past and the response by maintainers is consistently that subtype reduction, while sometimes inconvenient, is not only not considered to be a bug, but completely by design, so it's very unlikely you're going to get much traction on this. #53425 (comment)

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

No branches or pull requests

4 participants