Skip to content

FlattenDeep

Since 1.4.0

Recursively flattens a nested tuple to any depth, producing a flat tuple of all elements.

Signature

typescript
export type FlattenDeep<T extends unknown[], Acc extends unknown[] = []> = T extends [
	infer First,
	...infer Rest,
]
	? First extends unknown[]
		? FlattenDeep<Rest, [...Acc, ...FlattenDeep<First>]>
		: FlattenDeep<Rest, [...Acc, First]>
	: Acc

Parameters

ParameterDescription
TThe nested tuple to flatten recursively
AccInternal accumulator for building the result (default: [])

Examples

Basic Usage

typescript
import type { FlattenDeep } from 'uni-types'

type Result = FlattenDeep<[1, [2, [3, [4, [5]]]]]> // [1, 2, 3, 4, 5]

With Multiple Nesting Levels

typescript
import type { FlattenDeep } from 'uni-types'

type Result = FlattenDeep<[['a', ['b']], [['c', ['d', ['e']]]]]> // ['a', 'b', 'c', 'd', 'e']
  • Flatten - Flatten nested tuple (one level)
  • Reverse - Reverse a tuple
  • Unique - Remove duplicates from tuple

Released under the MIT License.