UNPKG

ts-type-forge

Version:

[![npm version](https://img.shields.io/npm/v/ts-type-forge.svg)](https://www.npmjs.com/package/ts-type-forge) [![npm downloads](https://img.shields.io/npm/dm/ts-type-forge.svg)](https://www.npmjs.com/package/ts-type-forge) [![License](https://img.shields.

30 lines (28 loc) 1.33 kB
/** * Creates a readonly tuple containing a sequence of number literals from 0 up to (but not including) `N`. * Requires `N` to be a non-negative integer literal for which `MakeTuple<unknown, N>` can be computed. * * @template N - The upper bound (exclusive) of the sequence. Must be a non-negative integer literal. * @returns A readonly tuple type `readonly [0, 1, ..., N-1]`. * @example * type S3 = Seq<3>; // readonly [0, 1, 2] * type S0 = Seq<0>; // readonly [] * type S1 = Seq<1>; // readonly [0] */ type Seq<N extends number> = TSTypeForgeInternals.SeqImpl< MakeTuple<unknown, N> >; declare namespace TSTypeForgeInternals { /** * @internal * Implementation detail for `Seq`. Takes a tuple `T` and extracts its numeric indices as number literals into a new tuple. * @template T - A readonly tuple type (typically generated by `MakeTuple`). * @returns A readonly tuple containing the numeric indices of `T`. */ type SeqImpl<T extends readonly unknown[]> = { // Map over each key 'i' in the tuple T. readonly [i in keyof T]: i extends `${number}` // Check if the key is a numeric string (e.g., "0", "1"). ? ToNumber<i> // If yes, convert the string key to a number literal. : never; // If no (e.g., 'length', array methods), map to never (effectively filtered out). }; }