-
Notifications
You must be signed in to change notification settings - Fork 23
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
Make zip and zipAll typings generic #432
base: trunk
Are you sure you want to change the base?
Conversation
Codecov Report
@@ Coverage Diff @@
## trunk #432 +/- ##
=======================================
Coverage 99.21% 99.21%
=======================================
Files 221 221
Lines 1792 1792
=======================================
Hits 1778 1778
Misses 14 14 Continue to review full report at Codecov.
|
@conartist6 Are you interested in getting this merged? Let me know if there's anything I can fix or change. |
Sorry I actually hadn't looked at it yet. I'm need to play with it a bit to know if I'm comfortable with that definition.
|
This doesn't look to me like it should work, but the tests are passing. How can it work if you aren't ever actually inferring anything from tuple types? |
Sorry, I don't fully understand your question, but |
Ooooh yep when I dig into the way the test case is passing it's because the result of zip is I don't exactly understand how Typescript came up with I think what you should be leveraging is rest elements in tuple types. The problem that I think you're going to hit is that I can't find a way that works to map tuple types. The problem with using the mapped object type syntax that you were using is that the result looks kind of a little like a tuple, but also is wildly different from what typescript thinks of as a tuple type. Here is what that looks like in the playground. |
And regarding rest tuple types, yes, I am using rest tuple types in that definition. I just split it out into the declare function zip<Sources extends Wrappable<unknown>[]>(
...sources: Sources
): IterableIterator<{
[index in keyof Sources]: Unwrap<Sources[index]>;
}>; |
Right but what you wrote essentially boils down to declare function zip<T>(...sources: Array<Iterable<T>>); So sources is in an array type, not a tuple type. Once the tuple type becomes an array type you've lost the distinction between the types of any individual keys. |
Yes, I agree that function identity<T extends Array<unknown>>(...input: T): T {
return input;
}
const result = identity(1, 'a'); // type is [number, string] Sorry, if I'm missing something. Can you give me a code example where this breaks down? Or back to the original issue, show where the result is |
So let's talk about a simpler version of the identity function: declare function identity<T>(t: T): T; The way generics work is that the caller understands what the real type of So what you have to keep in mind is that when you write |
Oh man, to be honest I still can't tell if my logic is entirely straight on this. |
OOOOooooh. I did something stupid. I'm so sorry. I was using my IDE to look at the types internal to the type test, and it was saying any because it thought there was an error in |
I believe I have found a way to type
zip
andzipAll
with an arbitrary number of arguments. Let's get another set of eyes on this, but if it works, it's much cleaner than special cases for the two and three argument cases.