Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 22 additions & 10 deletions array-flatten.d.ts
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']
Copy link
Owner

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.

Copy link
Owner

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.

Copy link
Contributor Author

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
  : // ...

Copy link
Owner

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.

Copy link
Contributor Author

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

Copy link
Owner

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.


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