Array Manipulation Types
In one line
TypeScript can manipulate tuple types at compile time: append, prepend, filter, reverse, flatten.
In simple words
TypeScript's type system can manipulate tuples at the type level using variadic tuples and conditional types. You can implement array operations that run entirely at compile time:
- Push/Append —
[...T, Item] - Pop — remove last element with infer
- Reverse — recursively swap first and rest
- Filter — conditionally include elements
- Flatten — recursively spread nested tuples
- Map — transform each element type
These are useful for building type-safe APIs, generating method signatures, and transforming schema types. The recursion depth is limited (~50 levels) but sufficient for practical use.
Example
// Type-level: append to tuple
type Push<T extends unknown[], V> = [...T, V];
type R1 = Push<[1, 2, 3], 4>; // [1, 2, 3, 4]
// Type-level: reverse a tuple
type Reverse<T extends unknown[]> =
T extends [infer Head, ...infer Rest]
? [...Reverse<Rest>, Head]
: [];
type R2 = Reverse<[1, 2, 3]>; // [3, 2, 1]
// Type-level: flatten one level
type Flatten<T extends unknown[]> =
T extends [infer Head, ...infer Rest]
? Head extends unknown[]
? [...Head, ...Flatten<Rest>]
: [Head, ...Flatten<Rest>]
: [];
type R3 = Flatten<[1, [2, 3], 4]>; // [1, 2, 3, 4]These types manipulate tuples at compile time: Push appends, Reverse recursively reverses, Flatten spreads nested arrays. All with zero runtime code.
Common confusions
Confused with: array methods
The difference: Array methods (.push(), .reverse(), .flat()) operate on values at runtime. Array manipulation types operate on tuple types at compile time — no runtime code. They transform type-level tuples; array methods transform runtime arrays.
Related terms
Related Resources
Related Lessons
- TypeScript Advanced Types
Array types.
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.