You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Conditional type distribution leads to undesirable behavior for booleans #22596
changed the title [-]Distribution over unions leads to undesirable behavior for booleans[/-][+]Conditional type distribution leads to undesirable behavior for booleans[/+]on Mar 15, 2018
Like, you're suggesting that we need to start interpreting string | boolean as different than string | true | false, effectively. Your example isn't really unique to booleans.... If you had any aliased union and said Foo<string | Alias> you could argue that it's unexpected that alias got flattened into the top level union and mapped over.
Prior to distributive unions there wasn't really a place where a flattened union and an unflattened union would be perceived to behave differently, were there?
Yeah, but I'm struggling to come to terms with the implications, since without just treating boolean as not-a-union (which may have some undesired effects), I don't see how you'd distinguish the intent of
I think we have a conflict between "Conditional types should always distribute over unions" and "Primitives (where possible) should behave the same as a union of all their possible values". e.g.:
typeArrayify<T>=TextendsArray<any> ? T : T[];// Errorconstb1: Arrayify<boolean>=[true,false];// OKconsts1: Arrayify<string>=["a","b"];
The fact that the unit type boolean has a finite number of values shouldn't lead to magically different behavior compared to its infinitely-domained counterparts. This is especially confusing because the user never wrote a union type in the above example.
I also don't like the fact that a conditional type does not behave the same as if you wrote its in-place substitution:
Primitives (where possible) should behave the same as a union of all their possible values
Is this not already thrown out with narrowing? x === true ? x : x narrows in the else expression but x === 0 ? x : x does not. The same applies for string, and also mapped types such as {[K in string]: K}. (I appreciate you said where possible so me picking out edge-cases isn't entirely fair).
Is this a reasonable summary of options?
Distribute over unions but exclude special cases such asboolean.
Downside being boolean is no longer equivalent to true | false for conditionals. There are other cases where you might want to prevent distribution for non-primitive such as your example where this wouldn't help.
Provide some built-in type that prevents lifting.
Lifting can be prevented currently using tuples but this might clash with actual uses of tuples. Doing this also loses constraints that should be constructed by the condition being satisfied.
Don't distribute.
Given that it's very useful and can't be encoded (I think) this seems a non-option.
Distribute over all primitives.
This would make conditional types depart from the behavior of narrowing (though I think narrowing could use conditional types). While consistent it doesn't help with the fact that users may want boolean to be atomic. Would also require some built-in notion of a complement type, at least of the primitives. I think there would also be some issues with the completeness of extends.
Given that boolean is simply an alias for true | false, it is hard to see by what rationale we'd not distribute over boolean but still distribute over 0 | 1 or "yes" | "no".
The issue here really isn't with boolean but rather with string and number. They represent infinite domains of values over which we cannot distribute operations, so by necessity they end up behaving differently. Even if we had "exclusion types" such as string except "a" | "b" they'd still behave differently.
I don't think there's any way we could magically get this "right" because the definition of right is all in the eye of the beholder. Sometimes you want union types to distribute, sometimes you don't. The important thing is that we be consistent in when we distribute and that we allow you to opt out:
We distribute over union types only when the leftmost type in a conditional type is a naked type parameter.
You can opt out by applying some covariant type constructor to the two types in the extends clause. For example [T] extends [undefined] ? X : Y checks whether T is exactly the type undefined, i.e. it would be true for undefined but not for string | undefined.
Here's one way to write the original example where you explicitly control which types are grouped:
Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.
Activity
[-]Distribution over unions leads to undesirable behavior for booleans[/-][+]Conditional type distribution leads to undesirable behavior for booleans[/+]weswigham commentedon Mar 15, 2018
But Daniel,
type Myboolean = true | falseneeds to behave the same way asboolean, since it's exactly the same type.weswigham commentedon Mar 15, 2018
Like, you're suggesting that we need to start interpreting
string | booleanas different thanstring | true | false, effectively. Your example isn't really unique to booleans.... If you had any aliased union and saidFoo<string | Alias>you could argue that it's unexpected that alias got flattened into the top level union and mapped over.weswigham commentedon Mar 15, 2018
Prior to distributive unions there wasn't really a place where a flattened union and an unflattened union would be perceived to behave differently, were there?
DanielRosenwasser commentedon Mar 15, 2018
Hey, I get why it happens, but you clearly see why most users wouldn't want this, right?
weswigham commentedon Mar 15, 2018
Yeah, but I'm struggling to come to terms with the implications, since without just treating boolean as not-a-union (which may have some undesired effects), I don't see how you'd distinguish the intent of
from the intent of your example.
falsandtru commentedon Mar 15, 2018
Daniel Rosenwasser (@DanielRosenwasser) means this but this is very hard work.
jack-williams commentedon Mar 15, 2018
This is what I use as a work around:
The main problem with this is that
[U] extends [T]wont addU extends Tconstraints in the true branch.RyanCavanaugh commentedon Mar 15, 2018
I think we have a conflict between "Conditional types should always distribute over unions" and "Primitives (where possible) should behave the same as a union of all their possible values". e.g.:
The fact that the unit type
booleanhas a finite number of values shouldn't lead to magically different behavior compared to its infinitely-domained counterparts. This is especially confusing because the user never wrote a union type in the above example.I also don't like the fact that a conditional type does not behave the same as if you wrote its in-place substitution:
falsandtru commentedon Mar 15, 2018
I think so too. I think Conditional type shouldn't distribute normal types to literal types without matching literal types.
Another point:
booleanmatches own literal types butnumberdoesn't.T extends 0 ? T : Tshould return0 | Rest<0>(it will be unified tonumber) if possible.jack-williams commentedon Mar 16, 2018
Ryan Cavanaugh (@RyanCavanaugh)
Is this not already thrown out with narrowing?
x === true ? x : xnarrows in the else expression butx === 0 ? x : xdoes not. The same applies forstring, and also mapped types such as{[K in string]: K}. (I appreciate you said where possible so me picking out edge-cases isn't entirely fair).Is this a reasonable summary of options?
boolean.Downside being
booleanis no longer equivalent totrue | falsefor conditionals. There are other cases where you might want to prevent distribution for non-primitive such as your example where this wouldn't help.Lifting can be prevented currently using tuples but this might clash with actual uses of tuples. Doing this also loses constraints that should be constructed by the condition being satisfied.
Given that it's very useful and can't be encoded (I think) this seems a non-option.
This would make conditional types depart from the behavior of narrowing (though I think narrowing could use conditional types). While consistent it doesn't help with the fact that users may want
booleanto be atomic. Would also require some built-in notion of a complement type, at least of the primitives. I think there would also be some issues with the completeness ofextends.ahejlsberg commentedon Mar 16, 2018
Given that
booleanis simply an alias fortrue | false, it is hard to see by what rationale we'd not distribute overbooleanbut still distribute over0 | 1or"yes" | "no".The issue here really isn't with
booleanbut rather withstringandnumber. They represent infinite domains of values over which we cannot distribute operations, so by necessity they end up behaving differently. Even if we had "exclusion types" such asstring except "a" | "b"they'd still behave differently.I don't think there's any way we could magically get this "right" because the definition of right is all in the eye of the beholder. Sometimes you want union types to distribute, sometimes you don't. The important thing is that we be consistent in when we distribute and that we allow you to opt out:
extendsclause. For example[T] extends [undefined] ? X : Ychecks whetherTis exactly the typeundefined, i.e. it would be true forundefinedbut not forstring | undefined.Here's one way to write the original example where you explicitly control which types are grouped:
Now, I'm still not sure what you'd actually do with this type.
Regarding Ryan Cavanaugh (@RyanCavanaugh)'s example above, it seems to me that you'd never want arrayification to be distributive, so:
Griffork commentedon Mar 19, 2018
Please see #22630 for the example.
This current implementation makes booleans not assignable to booleans without a cast.
typescript-bot commentedon May 9, 2018
Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.