Discriminated union containing optional properties and generic types is not narrowed #55036
Labels
Possible Improvement
The current behavior isn't wrong, but it's possible to see that it might be better in some cases
Milestone
Bug Report
I would have expected
doesNotNarrow
(below) to also be able to narrowx.b
toNonUndefined<B>
.🔎 Search Terms
generic, narrow, discriminated, union, optional
🕗 Version & Regression Information
This is the behavior in every version I tried, and I reviewed all the FAQ for entries.
⏯ Playground Link
Playground link with relevant code
💻 Code
🙁 Actual behavior
x.b
was not narrowed.🙂 Expected behavior
I expected
x.b
to be narrowed because ifx.a
isundefined
, thenx
must be the latter part of the discriminated union becausex.a
can only beundefined
there. TypeScript seems to be able to work that out when the type parameters are constant/non-generic (seedoesNarrow
), but for some reason it's not able to work it out for the generic case (even thoughNonUndefined
should make it feasible).The text was updated successfully, but these errors were encountered: