-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Intersection of arrays #41874
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
Related to #39693 |
Array intersection is weird since there are many invariants of arrays and many invariants of intersections that can't be simultaneously met. For example, if it's valid to call In higher-order the current behavior is really the best we can do; in zero-order it's really preferable to just write |
This seems to also affect readonly arrays, though they do not allow mutations like push. |
@RyanCavanaugh Does the |
Basically, yes. |
@RyanCavanaugh I see, thanks for clarification! although I have 2 more questions:
|
|
@RyanCavanaugh Thanks for the answers |
I found the workaround way to solve an issue hope it helps someone who needs the same use case like mine |
@kasamachenkaow awesome! It's exactly what I was looking for 🥳 |
I found this issue while searching for a problem that I am having with refining the type of an array. Your examples showed me that the order matters, which is wild. I never would have thought that the order of an intersection type would make a difference! I was able to fix my particular error by swapping the order of
|
Search Terms
intersection array
Suggestion
Intersections of arrays are weird in my opinion. In TypeScript 4.1, only the first part of the intersection is considered. I think TypeScript should merge the array types, it would be especially convenient for objects:
Playground to try it out.
Use Cases
I want to be able to cherry-pick some paths from an interface. I have an Elasticsearch cluster and a TypeScript interface for the shape of the documents in the cluster.
With Elasticsearch, I'm able to retrieve only some values with their paths.
Using features introduced in TypeScript 4.1 I'm able to generate a type based on the interface and the source fields. And it works almost perfectly except for arrays. While it works fine with nested objects, and nullable and optional keys.
Usage:
PartialObjectFromSourceFields
declaration:But because of the limitation on array intersection, as soon as 2 paths on an array key are used, only the first one is considered:
Playground demo
Examples
I think the same merging rules should apply as without arrays:
Checklist
My suggestion meets these guidelines:
I don't know if it would be a breaking change 🤔 I wonder if it might conflict with this behavior #38348
The text was updated successfully, but these errors were encountered: