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
Unable to unify different uses of same conditional type #23541
I would expect that typescript should be able to infer that the return type of each case in the switch statement is consistent with what the conditional type returns for the given T
Actual behavior:
Type 'Whatever<string>' is not assignable to type 'TypeFromOption<T>'
Type 'Whatever<number>' is not assignable to type 'TypeFromOption<T>'
Adding as any after return a and return b causes it to work as expected and infer the correct return type for the function.
Union-constrained generics aren't narrowed, so we don't change the type of type in the switch and don't have any chance to even realize the return statements refer to different cases. I'm not sure there's any concrete next step we could do to fix this - in general we cannot reason very strongly about a type parameter because we can't reason about a domain that doesn't exist yet, though literal-union-constrained generics might be a special case. But there's no mechanism in place right now to unify the flow control behavior with the conditional types - without more scenarios I think this is pretty far out of scope.
Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.
Activity
RyanCavanaugh commentedon Apr 19, 2018
Union-constrained generics aren't narrowed, so we don't change the type of
typein theswitchand don't have any chance to even realize the return statements refer to different cases. I'm not sure there's any concrete next step we could do to fix this - in general we cannot reason very strongly about a type parameter because we can't reason about a domain that doesn't exist yet, though literal-union-constrained generics might be a special case. But there's no mechanism in place right now to unify the flow control behavior with the conditional types - without more scenarios I think this is pretty far out of scope.#23132 might be related
leighman commentedon Apr 20, 2018
Yeh, thought typescript might be able to do more because it was a union of literals.
Thanks for the info.
typescript-bot commentedon May 4, 2018
Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.