-
-
Notifications
You must be signed in to change notification settings - Fork 19
feat: improved types #21
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
Conversation
| : Element | ||
| : never | ||
| tuple: InferElement<Array<T[number]>> | ||
| }[Array<T[number]> extends T ? 'array' : 'tuple'] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What was the reasoning behind this switch between tuple and array behavior? I wasn't able to replicate the reason this was here when testing different solutions.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
3.7 included recursive types, but it didn't improve for within conditions - I was still getting the same error when testing this out. It's the main reason I still need to do:
{
[K in Extract<keyof T, number>]: PickValue<T[K]>;
}[number]As long as it wasn't any sort of explicit tuple support, that's fine - I made sure I have tests for tuples on master but wasn't sure if this did something I failed to test.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Did you intentionally exclude the arguments support?
T extends IArguments
? unknown
: // ...There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@aleclarson Not intentionally, it wasn't clear that line was there to just make it be the unknown type instead of any. I'm not sure how useful it is to make it unknown now that I think about it, but there's definitely a test for arguments on master that passes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
unknown forces you to use type guards or an explicit typecast, so less runtime errors
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I’m aware of how it works. To clarify, I don’t see the point of changing a type on someone in this library if they’re using something unsafe - otherwise we could change all any types. I’d hope arguments usage is an edge case in TS, and by putting it in this library it’s condoning the usage arguments as the input type - which I believe it shouldn’t be used as for performance reasons.
|
I believe I was able to simplify this a bit (after my first attempt using 3.7 recursive type features failed on export type PickValue<T> = T extends ReadonlyArray<any>
? {
[K in Extract<keyof T, number>]: PickValue<T[K]>;
}[number]
: T;
export type FlatArray<T extends ArrayLike<any>> = Array<PickValue<T[number]>>; |
The current types were broken in TS 3.6+ so I partially rewrote them.
And
fromDepthwas misspelleddepthFrom.