|
Last change
on this file was a762898, checked in by istevanoska <ilinastevanoska@…>, 5 months ago |
|
Added visualizations
|
-
Property mode
set to
100644
|
|
File size:
883 bytes
|
| Line | |
|---|
| 1 | /**
|
|---|
| 2 | * Utility type for recursively unpacking nested array types to extract the type of the innermost element
|
|---|
| 3 | *
|
|---|
| 4 | * @example
|
|---|
| 5 | * ExtractNestedArrayType<(number | (number | number[])[])[]>
|
|---|
| 6 | * // number
|
|---|
| 7 | *
|
|---|
| 8 | * ExtractNestedArrayType<(boolean | (string | number[])[])[]>
|
|---|
| 9 | * // string | number | boolean
|
|---|
| 10 | */
|
|---|
| 11 | type ExtractNestedArrayType<T> = T extends ReadonlyArray<infer U> ? ExtractNestedArrayType<U> : T;
|
|---|
| 12 | /**
|
|---|
| 13 | * Flattens all depths of a nested array.
|
|---|
| 14 | *
|
|---|
| 15 | * @template T - The type of elements within the array.
|
|---|
| 16 | * @param {T[]} arr - The array to flatten.
|
|---|
| 17 | * @returns {Array<ExtractNestedArrayType<T>>} A new array that has been flattened.
|
|---|
| 18 | *
|
|---|
| 19 | * @example
|
|---|
| 20 | * const arr = flattenDeep([1, [2, [3]], [4, [5, 6]]]);
|
|---|
| 21 | * // Returns: [1, 2, 3, 4, 5, 6]
|
|---|
| 22 | */
|
|---|
| 23 | declare function flattenDeep<T>(arr: readonly T[]): Array<ExtractNestedArrayType<T>>;
|
|---|
| 24 |
|
|---|
| 25 | export { type ExtractNestedArrayType, flattenDeep };
|
|---|
Note:
See
TracBrowser
for help on using the repository browser.