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

Conditional type simplified too early #46761

Closed
webstrand opened this issue Nov 10, 2021 · 3 comments Β· Fixed by #53067
Closed

Conditional type simplified too early #46761

webstrand opened this issue Nov 10, 2021 · 3 comments Β· Fixed by #53067
Assignees
Labels
Fix Available A PR has been opened for this issue Needs Investigation This issue needs a team member to investigate its status. Rescheduled This issue was previously scheduled to an earlier milestone

Comments

@webstrand
Copy link
Contributor

Bug Report

πŸ”Ž Search Terms

conditional tuple extend simplify type parameter constraint

πŸ•— Version & Regression Information

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about conditional types

⏯ Playground Link

Playground link with relevant code

πŸ’» Code

type Bit = 0 | 1;
type And<A extends Bit, B extends Bit> = [A, B] extends [1, 1] ? 1 : 0;

type Test<A extends Bit, B extends Bit> = And<A extends 1 ? 0 : 1, B extends 1 ? 0 : 1>;
//    ^? type Test<I1 extends Bit, I2 extends Bit> = 1

type a = Test<1, 1>; // 1, should be 0

πŸ™ Actual behavior

Test<A, B> is being incorrectly simplified to 1 before being called, and Test<1, 1> is resolving incorrectly to 1.

πŸ™‚ Expected behavior

Test<A, B> should not be simplified, and Test<1, 1> should resolve to 0.

@andrewbranch
Copy link
Member

Your inner conditional types being fed to And are distributive. Making them not distributive fixes it. Still, it feels odd to me. @weswigham is this expected behavior?

@webstrand
Copy link
Contributor Author

webstrand commented Nov 10, 2021

Some more weird behavior: If I extract the conditional out into an alias, Test1<A,B> still gets simplified to 1, but if I add another branch to that conditional, Test2<A,B> is not simplified: playground The latter should still be distributive, right?

@whzx5byb
Copy link

If you make And like this, Test<A, B> will resolve to 0. That's weird.

type And<A extends Bit, B extends Bit> = A extends 1 ? B extends 1 ? 1 : 0 : 0;

@andrewbranch andrewbranch added the Needs Investigation This issue needs a team member to investigate its status. label Nov 12, 2021
@andrewbranch andrewbranch added this to the TypeScript 4.6.0 milestone Nov 12, 2021
@typescript-bot typescript-bot added the Fix Available A PR has been opened for this issue label Dec 9, 2021
@RyanCavanaugh RyanCavanaugh added the Rescheduled This issue was previously scheduled to an earlier milestone label May 13, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Fix Available A PR has been opened for this issue Needs Investigation This issue needs a team member to investigate its status. Rescheduled This issue was previously scheduled to an earlier milestone
Projects
None yet
6 participants