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

Unable to infer the inner type within expansion of a nested union #26807

Closed
pelotom opened this issue Aug 31, 2018 · 6 comments
Closed

Unable to infer the inner type within expansion of a nested union #26807

pelotom opened this issue Aug 31, 2018 · 6 comments
Labels
Fixed A PR has been merged for this issue

Comments

@pelotom
Copy link

pelotom commented Aug 31, 2018

TypeScript Version: 3.0.1, 3.0.3, 3.1.0-dev.20180831

Search Terms: infer nested union type

Code

type Maybe<A> = { tag: 'none' } | { tag: 'some'; value: A };
type MaybeMaybeNumber =
  | { tag: 'none' }
  | {
      tag: 'some';
      value: { tag: 'none' } | { tag: 'some'; value: number };
    };
type X = Maybe<Maybe<number>> extends Maybe<Maybe<infer T>> ? T : never;
type Y = MaybeMaybeNumber extends Maybe<Maybe<infer T>> ? T : never;

Expected behavior:

The types X and Y should both be number, because MaybeMaybeNumber is just an expansion of Maybe<Maybe<number>>.

Actual behavior:

X is number, but Y is {}.

Playground Link

@pelotom pelotom changed the title Unable to infer the inner type within a nested union Unable to infer the inner type within expansion of a nested union Aug 31, 2018
@ahejlsberg
Copy link
Member

This is a design limitation of our inference algorithm. Specifically, if, during inference, we are already processing another target type with the same associated symbol (such as an instantiation of the same generic type), we do not explore the nested target as it rarely yields further inferences, but definitely generates lots of work. Obviously that's not a good strategy here.

@pelotom
Copy link
Author

pelotom commented Sep 1, 2018

I see. Any chance of the design being revisited? 🙂

@sledorze
Copy link

@ahejlsberg is there any way to force that exploration somehow? (forcing being enforced by library authors at definition site)

@RyanCavanaugh RyanCavanaugh added the Design Limitation Constraints of the existing architecture prevent this from being fixed label Sep 17, 2018
@safareli
Copy link

safareli commented May 6, 2021

I've checked and starting from Typescript@4.1.5 this is fixed see playground link

/cc @pelotom

@vaaas
Copy link

vaaas commented May 15, 2022

Since this is fixed, this issue shouldn't be open, no?

@RyanCavanaugh
Copy link
Member

Yep!

@RyanCavanaugh RyanCavanaugh added Fixed A PR has been merged for this issue and removed Design Limitation Constraints of the existing architecture prevent this from being fixed labels May 19, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Fixed A PR has been merged for this issue
Projects
None yet
Development

No branches or pull requests

6 participants