-
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
types of A[] || B[]
and A[] ?? B[]
are incorrect if A
is a subtype of B
#54409
Comments
Subtype reduction is intended behavior. Duplicate of #46449 and others - search issues for βsubtype reductionβ. This comes up quite often and should probably be mentioned in the FAQ at this point. |
@fatcerberus thank you for the fast reply! I can see now it's an unfortunate design bug. I've added "subtype reductionβ to the Search Terms list |
It's admittedly annoying, but not really a bug. Technically the types |
@fatcerberus I'm afraid I have to disagree with your argument about the safety of the union of subtypes vs. the union of non-subtypes because your logic can be applied the same way to the non-subtype case: type A = { a: string }
type B = { b: number }
function foo (x: A | B): string {
if ('a' in x) {
return x.a.toUpperCase()
}
return `${x.b}`
}
const runtimeCrashArg = {a: true, b: 22}
foo(runtimeCrashArg) // no error Any union type allows such kind of type violation regardless of if the union members are bivariant or not |
Yes, |
Ah, this is what I was referring to: #53425 (comment) |
This issue has been marked as 'Not a Defect' and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
Bug Report
π Search Terms
Arrays, null coalesce, OR operator, subtype reduction
π Version & Regression Information
β― Playground Link
Playground link with relevant code
π» Code
π Actual behavior
bad
is{ x: string }[]
(type ofa
but withoutundefined
)π Expected behavior
bad
should be{ x: string }[] | { x: string, y: number }[]
(a union ofa
andsubA
withoutundefined
).Type of
||
should not depend on the relationship between types of operandsNotes
null coalesce
??
suffers from the same problemThe text was updated successfully, but these errors were encountered: