UNPKG

rivo

Version:

🤖 The ultimate library you need for composable type-level programming in TypeScript, powered by HKT.

103 lines (97 loc) • 2.5 kB
import type AppendFn from "./Append"; import type { MapEachFn } from "./MapEach"; import type Of1Fn from "./Of1"; import type { RepeatFn } from "./Repeat"; import type _0Fn from "./_0"; import type _1Fn from "./_1"; import type { Fn1, PartialApply } from "../HKT"; import type { Nat } from "../Num"; /*********** * Methods * ***********/ /** * Methods for tuples with more precise typing. */ export namespace Tuple { /** * [Fn] Get the element at index 0 of a tuple. * * Sig: `<TS extends readonly [unknown, ...unknown[]]>(xs: TS) => TS[0]` */ export type _0 = _0Fn; /** * [Fn] Get the element at index 1 of a tuple. * * Sig: `<TS extends readonly [unknown, unknown, ...unknown[]]>(xs: TS) => TS[1]` */ export type _1 = _1Fn; /** * [Fn] Append an element to a tuple. * * Sig: `<TS extends readonly unknown[], U>[y: U](xs: TS) => [...TS, U]` */ export type Append<U> = PartialApply<AppendFn, [U]>; /** * [Fn] Map each element of a tuple by a list of branching functions and returns a new tuple. * * Sig: `<A, B, ..., X1, X2, ...>[...branches: [(a: A) => X1, (b: B) => X2, ...]](xs: [A, B, ...]) => [X1, X2, ...]` */ export type MapEach< Branch1 extends Fn1, Branch2 extends Fn1, Branch3 extends Fn1 = never, Branch4 extends Fn1 = never, Branch5 extends Fn1 = never, Branch6 extends Fn1 = never, Branch7 extends Fn1 = never, Branch8 extends Fn1 = never, Branch9 extends Fn1 = never, Branch10 extends Fn1 = never, Branch11 extends Fn1 = never, Branch12 extends Fn1 = never, Branch13 extends Fn1 = never, Branch14 extends Fn1 = never, Branch15 extends Fn1 = never, Branch16 extends Fn1 = never, > = MapEachFn< Branch1, Branch2, Branch3, Branch4, Branch5, Branch6, Branch7, Branch8, Branch9, Branch10, Branch11, Branch12, Branch13, Branch14, Branch15, Branch16 >; } /****************** * Static members * ******************/ /** * [Fn] Create a tuple with one element. * * Sig: `<T>(x: T) => readonly [T]` * * @example * ```typescript * type R1 = $<Tuple$$Of1, 1>; * // ^?: readonly [1] * type R2 = $<Tuple$$Of1, "foo" | false>; * // ^?: readonly ["foo" | false] * ``` */ export type Tuple$$Of1 = Of1Fn; /** * [Fn] Repeat `T` for `N` times. * * Sig: `<N extends Nat, T>[n: N](x: T) => readonly [T, T, ...]` */ export type Tuple$$Repeat<N extends Nat> = PartialApply<RepeatFn, [N]>;