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
This might be a duplicate of #33654 but I'm not completely sure. This issue has a simpler example anyway.
It also seems to be a duplicate of #9919 which was reportedly fixed. This is either regression or a missed case.
🕗 Version & Regression Information
This is the behavior in every version I tried (v3.3 and v4.5), and I reviewed the FAQ for entries about intersection with discriminated union.
This is the intended behavior. Through distributivity:
typeX=({a: string}|{b: number})&{a: unknown};// is equivalent totypeX=({a: string}&{a: unknown})|({b: number}&{a: unknown});// ^^^^^^^^^ this one ^^^^^^^^^^^^
so { b: 3, a: true } is a legal inhabitant of the second constituent of the union, so given an arbitrary value of type X you can't be guaranteed to get a string from it
What's different about the follow case then? How come TypeScript is able to determine that the resulting type is string here?
declareconstfoo: {a: string}|{b: number};declarefunctionhasA(value: unknown): value is typeofvalue&{a: unknown};if(hasA(foo)){typeZ=typeoffoo['a'];// string}
Bug Report
🔎 Search Terms
intersection with discriminated union
This might be a duplicate of #33654 but I'm not completely sure. This issue has a simpler example anyway.
It also seems to be a duplicate of #9919 which was reportedly fixed. This is either regression or a missed case.
🕗 Version & Regression Information
⏯ Playground Link
Playground link with relevant code
💻 Code
🙁 Actual behavior
type
Y
isunknown
.🙂 Expected behavior
type
Y
should bestring
.Why:
{ a: string } | { b: number }
with "a
must be defined as a field", we get{ a: string }
.{ a: string } & { a: unknown }
can be simplified to{ a: string }
The text was updated successfully, but these errors were encountered: