TypeScript Tuple Type Utilities
typescript-tuple is a type-only utility library providing a rich set of generics for manipulating tuple types in TypeScript. The current stable version is 5.0.1. It offers various type-level operations, mimicking common array methods like `Append`, `Prepend`, `Reverse`, `Concat`, `Slice`, `Drop`, and `FillTuple`, but applied to static tuple types. This allows developers to enforce strict type safety and leverage advanced type inference when working with fixed-length or variable-length tuple structures, catching potential type mismatches at compile time rather than runtime. The library differentiates itself by focusing exclusively on type transformations, offering a comprehensive suite for tuple manipulation within TypeScript's type system itself, making it a powerful tool for complex type definitions and functional type programming.
Common errors
-
Type instantiation is excessively deep and possibly infinite. [ts(2589)]
cause This error occurs when a type definition leads to a recursive evaluation that exceeds TypeScript's internal depth limit, often due to complex tuple operations with large numbers or problematic type arguments.fixReview the type arguments used for length-related generics (e.g., `Repeat`, `Drop`, `SliceStartQuantity`). Ensure positive integer literals are used. Simplify the overall type structure if possible, or break down complex operations into smaller steps. -
Argument of type '...' is not assignable to parameter of type '...'. [ts(2345)]
cause Mismatch between the expected input type for a generic (e.g., expecting a tuple, but receiving a non-tuple array) or trying to assign a value to a type that doesn't match the generic's output.fixVerify the type of the argument being passed to the generic matches its constraints. For output types, ensure the value being assigned strictly conforms to the type produced by the generic. Use `as const` on array literals to ensure they are inferred as tuples.
Warnings
- gotcha Using floating-point numbers or negative numbers as arguments for tuple length/index-related generics (e.g., `Repeat`, `Drop`, `SliceStartQuantity`) can lead to excessive type instantiation or infinite recursion within the TypeScript compiler.
- gotcha The library primarily operates on literal tuple types. When passing a variable or an array with a broader type (e.g., `number[]` instead of `[1, 2, 3]`), TypeScript's inference might default to array types, losing the specific tuple structure.
Install
-
npm install typescript-tuple -
yarn add typescript-tuple -
pnpm add typescript-tuple
Imports
- Append
const Append = require('typescript-tuple')import { Append } from 'typescript-tuple' - Reverse
import Reverse from 'typescript-tuple'
import { Reverse } from 'typescript-tuple' - IsFinite
import { IsFinite } from 'typescript-tuple'import type { IsFinite } from 'typescript-tuple'
Quickstart
import { Append, Reverse, Concat } from 'typescript-tuple';
// Define some base tuples with strict types
type MyTupleA = ['hello', 'world'];
type MyTupleB = [1, 2, 3];
// --- Example 1: Appending an element ---
// Creates a new tuple type with an additional element at the end.
type AppendedTuple = Append<MyTupleA, '!'>;
// Expect: ['hello', 'world', '!']
const appendedInstance: AppendedTuple = ['hello', 'world', '!'];
console.log('Appended Tuple:', appendedInstance);
// --- Example 2: Reversing a tuple ---
// Creates a new tuple type with elements in reverse order.
type ReversedTuple = Reverse<MyTupleB>;
// Expect: [3, 2, 1]
const reversedInstance: ReversedTuple = [3, 2, 1];
console.log('Reversed Tuple:', reversedInstance);
// --- Example 3: Concatenating two tuples ---
// Merges two tuple types into a single new tuple type.
type ConcatenatedTuple = Concat<MyTupleA, MyTupleB>;
// Expect: ['hello', 'world', 1, 2, 3]
const concatenatedInstance: ConcatenatedTuple = ['hello', 'world', 1, 2, 3];
console.log('Concatenated Tuple:', concatenatedInstance);
// This library operates purely at the TypeScript type level.
// The variables 'appendedInstance', 'reversedInstance', and 'concatenatedInstance'
// are runtime examples of how values conforming to these derived types would look.
// The type manipulations themselves do not generate any JavaScript runtime code.