-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
Type guard of union with conditional type not working since 4.3 #44382
Comments
@RyanCavanaugh Can this bug be prioritize for |
This doesn't meet our bar for a patch release, but I'll move it to 4.4 |
This was caused by #43183, because the conditional type I donβt know if it would make sense for your real code @pedro-pedrosa, but a workaround would be to expand the conditional type to a union in your type predicate: function f2<S>(u: A<S> | B<S> | C<S>): u is B<S> | C<S> {
return false
} The only way I can think to fix this without undoing the very useful improvements of #43183 would be to carry the original, non-substituted type through narrowing (in addition to the usually-more-narrowable constraint-substituted type), trying it if narrowing the constraint-substituted type had no effect. But that sounds like it could be expensive, for what feels like quite an edge case to me. |
Fortunately I was able to change it to Thanks for the suggestion. |
Coming back to this, I think my earlier analysis was right, and given the existence of a reasonable workaround, I doubt we will want to take on the complexity and performance costs of doing something different here. This is very similar to #47337, which we discussed in a recent design meeting: type Cond<S> = S extends number ? B<S> : C<S>
type U2<S> = A<S> | Cond<S>
+ function f2<S>(u: U2<S>): u is B<S> | C<S> {
- function f2<S>(u: U2<S>): u is Cond<S> {
return false
}
function test2<S>(x: U2<S>) {
if (!f2(x)) {
x.a //ERROR
}
} Each of the two signatures highlighted in this diff is reasonable to write, but they work mutually exclusively based on whether When we discussed #47337, @ahejlsberg noted that weβre doing with |
Bug Report
π Search Terms
type guard conditional type
π Version & Regression Information
β― Playground Link
Playground link with relevant code
π» Code
π Actual behavior
The type guard is unable to narrow the type of
x
toA
π Expected behavior
The type guard should narrow the type of
x
toA
because it is not assignable toCond<S>
The text was updated successfully, but these errors were encountered: