Registry / type-stubs / typescript-tuple

typescript-tuple

JSON →
library5.0.1jsnpmunverified

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.

type-stubs
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

This library consists solely of type declarations; there are no runtime values to import. CommonJS `require` will result in an empty object or a TypeScript error.

import { Append } from 'typescript-tuple'

All utilities are named exports. There is no default export. Attempting a default import will result in a TypeScript error.

import { Reverse } from 'typescript-tuple'

For explicit type-only imports in TypeScript 3.8+, using `import type` is preferred for clarity and ensures no runtime code is inadvertently bundled, though a regular `import` statement works for type declarations as well.

import type { IsFinite } from 'typescript-tuple'

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.

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.
Debug
Known footguns
gotchaUsing 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.
gotchaThe 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.
Upgrade
Version history

Breaking-change detection hasn't run for this library yet.

Audit
Security & dependencies

CVE tracking and dependency tree are planned for a later release.

Agent activity
0 hits · last 30 days

No traffic data recorded yet.

Resources