-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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
"number[] & [any]" should be "[number]" #37926
Comments
You could write Currently intersections don't have any special rules to produce new types; it's not clear this would be desirable - for an intersection of N elements we'd have to do N * (N - 1) comparisons to see if any "special" rules applied, for example. |
FWIW I think the issue boils down to: type A = Record<number,number> & [string];
type B = W[0] // string The indexed access type ignores the index signature rather than creating an intersection type from the property and the index type. In most cases this would be what you want (but not in this very specific case). |
Thanks for your prompt response @RyanCavanaugh and @jack-williams. :)
You've been in this for many more years than me, but given here N is the number of index types in the tuple (which should be manually typed, at least once), and is a compile-time, usually one-time-only operation, I really don't think the operation having a run-time complexity of O(N²) is a drawback for developers who get to here. Thanks for the
I didn't get this one, Jack. Hence, what can be concluded is the first index (index That's why to me there is only one interpretation behind this and I couldn't follow why would someone expect anything else from it. Thanks again! |
This issue has been marked 'Working as Intended' and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
I am under the impression that tuple is a fixed-length array. Given that, if I intersect a typed array with a fixed-length tuple, I should get a fixed-length and typed tuple, where the length is derived from the former and the type form the latter.
Unfortunately, it doesn't work as intended.
TypeScript Version: 3.8.3
Search Terms: tuple, array, intersection, union
Code
Playground Link
Expected behavior:
typeof x
should benumber
Actual behavior:
typeof x
isany
Playground Link:
Related Issues: I found #30895 but that's a different issue.
History/Context: I want to write a predicate for
isSingleElementArray(x: unknown)
function and I thoughtx is [unknown]
should work, but it doesn't. See the example below:Playground Link
If I change the predicate to
x is unknown[]
it works fine, but obviously it won't consider/communicate the length anymore.The text was updated successfully, but these errors were encountered: