-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Assignment of T[K] := T[K] when it's a union is too strictly checked #38762
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
Giving more thoughts, the following rule might be a good approximation? Assignment from type
Again, I might be missing some obvious things and would be happy to learn more about this problem. |
I guess this would also relate to #31445, which is the canonical issue around this meta issue. Except for this one, my use case is more on the type level, meaning both |
Indeed, this is just an alternate form of #31445; any statements about the correctness of this function are predicated on the same unmutated utterance of |
@RyanCavanaugh Any ETA for this issue? We’ve been stuck with 3.4.5 for almost a year because of this exactly problem |
@zen0wu |
@RyanCavanaugh Good to know about its complexity. Unfortunately ts-ignore is not an option for us. We use this pattern very extensively, where we have type-level Map that maps from a literal to a type of data model we have. Something like below. It's like a very central piece of our code base. It might also be worth to mention that, because we have many tables in this map, and this
|
This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
TypeScript Version: 3.9.2 and nightly
Search Terms: generic keys
Code
Expected behavior:
It should pass type checking. The code is perfectly fine since with whatever choice of K,
the target and the source of the assignment should have the exactly same type because
undefined
is stripped out from the source.Actual behavior:
TS reports
Type 'undefined' is not assignable to type 'never'.
I'm aware this is the consequence of #30769. Not sure what would be the best fix here, but intuitively even with the restricted behavior, assigning from X[K] to Y[K] seems to be valid, given
K extends keyof X & K extends keyof Y
The logic here is there's cases where we don't want
X[K]
becoming an intersection from a union, based on the source side of the assignment. Also it's very easy thatX[K]
become anever
and user will get confused about it.Playground Link:
Related Issues:
#32693, #31445
The text was updated successfully, but these errors were encountered: