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

Simple ternary inference issue with generics #27578

Closed
jeremybparagon opened this issue Oct 5, 2018 · 2 comments
Closed

Simple ternary inference issue with generics #27578

jeremybparagon opened this issue Oct 5, 2018 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@jeremybparagon
Copy link

TypeScript Version: 3.2.0-dev.20181004

Code

const f = <T>(a: number) => {
  const b: (T extends { } ? number : number) = a;
};

Expected behavior: Successful compilation

Actual behavior: I get the following error:

error TS2322: Type 'number' is not assignable to type 'T extends {} ? number : number'.

   const b: (T extends { } ? number : number) = a;
         ~

Playground Link: Link

Use case:
I wouldn't expect anybody to write the code above. Here's something more like my original example (link):

const f = <B extends true | false>(b: B, n: number): (true extends B ? number | null : number) => {
  if (b) {
    return null as true extends B ? null : never;
  }
  return n;
};

This gives the error

error TS2322: Type 'number' is not assignable to type 'true extends B ? number : number'.

   return n;
   ~~~~~~~~~

In this practical example, there is a workaround, changing the return type to number | (true extends B ? null : never). But it was hard for me to figure out what was making TypeScript unhappy, since this seemed too simple an inference for TypeScript to miss.

@jack-williams
Copy link
Collaborator

The first example is due to this #27530 I think.

The second example is covered by this #26933 I think.

@jeremybparagon
Copy link
Author

Both should be covered by #26933. Thanks.

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Oct 8, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

3 participants