ts-tuple-hacks icon indicating copy to clipboard operation
ts-tuple-hacks copied to clipboard

TypeScript collection of tuple tasks

NPM Version

ts-tuple-hacks

Implementation of some utility types for tuples.

Beautified version of typepark with TypeByPath & PathOf implemented.
Checkout Variadic Kinds thread.

Check also:

Install

npm i ts-tuple-hacks --save-exact

Methods

Pick head of tuple

type head = Head<[string, boolean, number]>;

head -> string

Pick tail of tuple

type tail = Tail<[string, boolean, number]>;

tail -> [boolean, number]

Push type in front of tuple

type tuple = Shift<string, [boolean, number]>;

tuple -> [string, boolean, number]

Push type at the end of tuple

type tuple = Push<string, [boolean, number]>;

tuple -> [boolean, number, string]

Pick last type of tuple

type last = Last<[boolean, number, string]>;

last -> string

Reverse tuple

type reversed = Reverse<[boolean, number, string]>;

reversed -> [string, number, boolean]

Compose (join) two tuples

type composed = Compose<[boolean, string], [number]>;

composed -> [boolean, string, number]

Pick type by path in object

const o = {x: { y: 10 }};

type xy = TypeByPath<typeof o, ['x', 'y']>;
xy -> number

type xyz = TypeByPath<typeof o, ['x', 'y', 'z']>;
xyz -> never

Pick path in object if it is valid
check also pathof.

const o = {x: { y: 10 }};

type xy = PathOf<typeof o, ['x', 'y']>;
xy -> ['x', 'y']

type xyz = PathOf<typeof o, ['x', 'y', 'z']>;
xyz -> never

Make pipe function type
Tuple represents results of each pipe's step

type MyPipe = PipeFunc<['a', 'b', 'c'], (a: number) => boolean>;

const pipe: MyPipe = undefined!;

const result = pipe(
    (x: (a: number) => boolean) => 'a',
    (x: 'a') => 'b',
    (x: 'b') => 'c',
);

result -> 'c'