-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Wrong inference of cross product if component union types exceed (small) cardinality #43108
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
Comments
Looks like related to #40803 |
Duplicate is correct. In general, |
Right, in general, |
This isn't unsoundness. Producing When comparing two types, there are several possible results:
There is an infinite class of pairs of types which might have the same inhabitants, but cannot be proven to in a reasonable period of time. What you're proposing is impossible per Gödel's incompleteness theorem. |
Sorry Ryan, I might not have expressed myself clearly. I do not propose that TypeScript overcomes the principled incompleteness that Gödel once had proved. I just tried to suggest the following: If I ask a question of the sort No Ryan, this behavior is not an instance of incompleteness, it is in fact one of unsoundness. If your compiler sometimes gives no answer (i.e. throws some kind of "too complex" or "unprovable" error), then it is incomplete. But if it gives wrong answers, as it is the case here, it is unsound. I don't want to rise an academic discussion about these two concepts. The point is rather that incompleteness is much more acceptable than unsoundness. And the kind of behavior under discussion here is definitely unacceptable to my mind. And again, no, Ryan: "Producing And by the way, the given code example above is not an example of a non-provable outcome. The opposite is true. Very elemental mathematics suffices to prove that the correct answer is |
If taking the |
This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
Bug Report
We have two union types
A = A1 | ... | Am
andB = B1 | ... | Bm
and construct the cross productA x B
as a mapped typeC
. If the cardinality ofn x m
exceeds a certain (low) limit, then the inferred cross product seems to miss some pairs. It worked fine forn = 4
,m = 6
and already failed forn = 5
,m = 6
.We compare the cross product type
C
with a coextensional expanded typeD
. We conclude in the failure casen = 5, m = 6
thatC
misses some pair members sinceD extends C ? true : false
evaluates tofalse
(while it evaluates totrue
for then = 4, m = 6
case) what signals thatC
has less members thanD
.🔎 Search Terms
cross product, union, inference
🕗 TypeScript versions 4.2.2, 4.1.5
Example code: The role of type
A
is played by typeRowObject
, the role ofB
byColumnObject
, the role ofC
byPositionAsCrossProduct
and the role ofD
byPosition
or equivalently bySinglePosition<Row, Column>
. TypeEQ
checks for coextensionality of types.⏯
Playground Link working example,
n = 4
(cardinality of typeRow
),m = 6
(cardinality of typeColumn
)💻 Code working example:
⏯
Playground Link failing example,
n = 5
,m = 6
💻 Code failing example:
🙁 Actual behavior
The types of variables
testPosition2
andtestPosition3
evaluate tofalse
, respectively.🙂 Expected behavior
The type of these two variables should evaluates to
true
since typesSinglePosition<Row, Column>
,Position
andPositionAsCrossProduct
are all coextensional, i.e. comprise the exact same values.The text was updated successfully, but these errors were encountered: