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.
npm install typescript-tupleVerified import paths — ran on the pinned version, not inferred.
Demonstrates fundamental type-level tuple manipulations: appending an element, reversing a tuple's order, and concatenating two tuples, showing how the library provides powerful type-safety for array-like operations on fixed-length type structures.
Always use positive integer literals for type parameters that define lengths or indices. Avoid `number` type for these parameters if possible, or be prepared for `any[]` or `T[]` as results when type recursion limits are hit.
Ensure that the input to these generics is a literal tuple type, or use `as const` assertions to allow TypeScript to infer the narrowest possible tuple type from an array literal (e.g., `['a', 'b'] as const`).
Review 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.
Verify 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.