typescript-tuple
- Version 5.0.1
- Published
- 42.5 kB
- 1 dependency
- MIT license
Install
npm i typescript-tuple
yarn add typescript-tuple
pnpm add typescript-tuple
Overview
Generics to work with tuples in TypeScript
Index
Type Aliases
type Append
type Append<Tuple extends any[], Addend> = Reverse<Prepend<Reverse<Tuple>, Addend>>;
Add an element to the end of a tuple
Example 1
Append<[0, 1, 2], 'new'> → [0, 1, 2, 'new']
type CompareLength
type CompareLength<Left extends any[], Right extends any[]> = utils.CompareLength< Left, Right>;
Compare length of two tuple
Example 1
CompareLength<[0, 1, 2], ['a', 'b', 'c']> → 'equal'
Example 2
CompareLength<[0, 1], ['a', 'b', 'c']> → 'shorterLeft'
Example 3
CompareLength<[0, 1, 2], ['a', 'b']> → 'shorterRight'
type Concat
type Concat<Left extends any[], Right extends any[]> = utils.Concat<Left, Right>;
Concat two tuple into one
Example 1
Concat<[0, 1, 2], ['a', 'b', 'c']> → [0, 1, 2, 'a', 'b', 'c']
type ConcatMultiple
type ConcatMultiple<TupleSet extends any[][]> = utils.ConcatMultiple<TupleSet>;
Concat multiple tuples
Example 1
ConcatMultiple<[], [0], [1, 2], [3, 4, 5]> → [0, 1, 2, 3, 4, 5]
type Drop
type Drop<Tuple extends any[], Quantity extends number> = utils.Drop< Tuple, Quantity>;
Drop
Quantity
first elements of a tupleExample 1
Drop<['a', 'b', 'c', 'd', 'e'], 2> → ['c', 'd', 'e']
Example 2
Drop<['a', 'b', 'c', 'd', 'e', ...string[]], 2> → ['c', 'd', 'e', ...string[]]
Example 3
Drop<['a', 'b', 'c', 'd', 'e', ...string[]], 10> → string[]
type FillTuple
type FillTuple<Tuple extends any[], Replacement> = utils.FillTuple< Tuple, Replacement>;
Fill a tuple of types
Example 1
FillTuple<[0, 1, 2], 'x'> → ['x', 'x', 'x']
Example 2
FillTuple<any[], 'x'> → 'x'[]
type FilterTuple
type FilterTuple<TupleSet extends any[], Mask> = utils.FilterTuple<TupleSet, Mask>;
Filter tuple elements thats match the mask
Example 1
FilterTuple<[1, 2, true, '3'], string | number> → [1, 2, "3"]
type First
type First<Tuple extends [any, ...any[]]> = Tuple[0];
Get type of first element
Example 1
First<[0, 1, 2]> → 0
type IsFinite
type IsFinite<Tuple extends any[], Finite = true, Infinite = false> = utils.IsFinite< Tuple, Finite, Infinite>;
Choose type base on whether or not a tuple is finite
Example 1
IsFinite<[0, 1, 2]> → true
Example 2
IsFinite<[0, 1, 2, ...number[]]> → false
Example 3
IsFinite<[0], 'Finite', 'Infinite'> → 'Finite'
Example 4
IsFinite<[0, ...number[]], 'Finite', 'Infinite'> → Infinite
type Last
type Last<Tuple extends any[]> = utils.Last<Tuple>;
Get type of last element
Example 1
Last<[0, 1, 2]> → 2
type LongestTuple
type LongestTuple<TupleSet extends [any[], ...any[][]]> = utils.LongestTuple<TupleSet>;
Find shortest tuple in a set of tuples
Example 1
LongestTuple<[[0, 1, 2], [true, false], ['a', 'b', 'c', 'd']]> → ['a', 'b', 'c', 'd']
type Prepend
type Prepend<Tuple extends any[], Addend> = utils.Prepend<Tuple, Addend>;
Add an element to the beginning of a tuple
Example 1
Prepend<[0, 1, 2], 'new'> → ['new', 0, 1, 2]
type Repeat
type Repeat<Type, Count extends number> = utils.Repeat<Type, Count, []>;
Repeat a certain type into a tuple
Example 1
Repeat<'foo', 4> → ['foo', 'foo', 'foo', 'foo'] To avoid potential infinite loop, Count must be an integer greater than or equal to 0
type Reverse
type Reverse<Tuple extends any[]> = utils.Reverse<Tuple>;
Reverse a tuple
Example 1
Reverse<[0, 1, 2]> → [2, 1, 0]
type ShortestTuple
type ShortestTuple<TupleSet extends [any[], ...any[][]]> = utils.ShortestTuple<TupleSet>;
Find shortest tuple in a set of tuples
Example 1
ShortestTuple<[[0, 1, 2], [true, false], ['a', 'b', 'c', 'd']]> → [true, false]
type SliceStartQuantity
type SliceStartQuantity< Tuple extends any[], Start extends number, Quantity extends number> = utils.SliceStartQuantity<Tuple, Start, Quantity>;
Slice a tuple
Example 1
SliceStartQuantity<[0, 1, 2, 3, 4, 5, 6], 2, 3> → [2, 3, 4]
Example 2
SliceStartQuantity<[0, 1, 2, 3, 4, 5, 6], 2, 9> → [2, 3, 4, 5, 6]
type SortTwoTuple
type SortTwoTuple< Left extends any[], Right extends any[], WhenEqual = [Left, Right]> = utils.SortTwoTuple<Left, Right, WhenEqual>;
Sort two tuples in order of [shorter, longer]
Example 1
SortTwoTuple<[0, 1, 2, 3], ['a', 'b']> → [['a', 'b'], [0, 1, 2, 3]]
Example 2
SortTwoTuple<[0, 1], ['a', 'b', 'c', 'd']> → [[0, 1], ['a', 'b', 'c', 'd']]
Example 3
SortTwoTuple<[0, 1, 2], ['a', 'b', 'c']> → [[0, 1, 2], ['a', 'b', 'c']]
Example 4
SortTwoTuple<[0, 1, 2], ['a', 'b', 'c'], 'EQUAL'> → 'EQUAL'
type Tail
type Tail<Tuple extends any[]> = utils.Tail<Tuple>;
Drop the first element
Example 1
Tail<[0, 1, 2, 3]> → [1, 2, 3]
Package Files (2)
Dependencies (1)
Dev Dependencies (6)
Peer Dependencies (0)
No peer dependencies.
Badge
To add a badge like this oneto your package's README, use the codes available below.
You may also use Shields.io to create a custom badge linking to https://www.jsdocs.io/package/typescript-tuple
.
- Markdown[![jsDocs.io](https://img.shields.io/badge/jsDocs.io-reference-blue)](https://www.jsdocs.io/package/typescript-tuple)
- HTML<a href="https://www.jsdocs.io/package/typescript-tuple"><img src="https://img.shields.io/badge/jsDocs.io-reference-blue" alt="jsDocs.io"></a>
- Updated .
Package analyzed in 4458 ms. - Missing or incorrect documentation? Open an issue for this package.