-
-
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,16 +1,28 @@ | ||
| declare function flatten <T> (array: flatten.NestedArray<T>): T[]; | ||
| declare type InferElement<T extends ArrayLike<any>> = T extends IArguments | ||
| ? unknown | ||
| : { | ||
| array: T[number] extends infer Element | ||
| ? Element extends ArrayLike<any> | ||
| ? InferElement<Element> | ||
| : Element | ||
| : never | ||
| tuple: InferElement<Array<T[number]>> | ||
| }[Array<T[number]> extends T ? 'array' : 'tuple'] | ||
|
|
||
| declare function flatten<T extends ArrayLike<any>>( | ||
| array: T | ||
| ): flatten.FlatArray<T> | ||
|
|
||
| declare namespace flatten { | ||
| export interface NestedArray <T> extends ReadonlyArray<T | NestedArray<T>> {} | ||
| export type FlatArray<T extends ArrayLike<any>> = Array<InferElement<T>> | ||
|
|
||
| export interface NestedList <T> { | ||
| [index: number]: T | NestedList<T>; | ||
| length: number; | ||
| } | ||
| export function depth<T extends ArrayLike<any>>( | ||
| array: T, | ||
| depth: number | ||
| ): FlatArray<T> | ||
|
|
||
| export function from <T> (array: NestedList<T>): T[]; | ||
| export function depth <T> (array: NestedArray<T>, depth: number): NestedArray<T>; | ||
| export function depthFrom <T> (array: NestedList<T>, depth: number): NestedArray<T>; | ||
| export function from<T>(array: ArrayLike<T>): T[] | ||
| export function fromDepth<T>(array: ArrayLike<T>, depth: number): T[] | ||
| } | ||
|
|
||
| export = flatten; | ||
| export = flatten | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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
tupleandarraybehavior? 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:
As long as it wasn't any sort of explicit tuple support, that's fine - I made sure I have tests for tuples on
masterbut 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
argumentssupport?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
unknowntype instead ofany. I'm not sure how useful it is to make itunknownnow that I think about it, but there's definitely a test for arguments onmasterthat 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.
unknownforces you to use type guards or an explicit typecast, so less runtime errorsThere 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.