types-belt
Version:
A comprehensive collection of TypeScript utility types for building robust and maintainable applications
182 lines • 6.64 kB
TypeScript
/**
* Array and tuple utility types for TypeScript
* @module ArrayTypes
*/
/**
* Extracts the element type of an array
* @template T - The array type
* @example
* ```typescript
* type StringArray = string[];
* type ElementType = ArrayElement<StringArray>; // string
*
* type NumberArray = Array<number>;
* type NumberElement = ArrayElement<NumberArray>; // number
* ```
*/
export type ArrayElement<T> = T extends readonly (infer U)[] ? U : never;
/**
* Creates a tuple type with a specific length and element type
* @template T - The element type
* @template L - The length
* @example
* ```typescript
* type StringTuple3 = Tuple<string, 3>; // [string, string, string]
* type NumberTuple5 = Tuple<number, 5>; // [number, number, number, number, number]
* ```
*/
export type Tuple<T, L extends number> = L extends L ? number extends L ? T[] : _TupleOf<T, L, []> : never;
type _TupleOf<T, L extends number, R extends unknown[]> = R['length'] extends L ? R : _TupleOf<T, L, [...R, T]>;
/**
* Creates a tuple type with optional elements
* @template T - The element type
* @template L - The length
* @example
* ```typescript
* type OptionalStringTuple = OptionalTuple<string, 3>; // [string?, string?, string?]
* ```
*/
export type OptionalTuple<T, L extends number> = Tuple<T | undefined, L>;
/**
* Creates a tuple type with the first element required and rest optional
* @template T - The element type
* @template L - The length
* @example
* ```typescript
* type HeadRequiredTuple = HeadRequiredTuple<string, 3>; // [string, string?, string?]
* ```
*/
export type HeadRequiredTuple<T, L extends number> = [T, ...Tuple<T | undefined, L extends 1 ? 0 : L extends 2 ? 1 : L extends 3 ? 2 : L extends 4 ? 3 : L extends 5 ? 4 : L extends 6 ? 5 : L extends 7 ? 6 : L extends 8 ? 7 : L extends 9 ? 8 : L extends 10 ? 9 : number>];
/**
* Extracts the first element type of a tuple
* @template T - The tuple type
* @example
* ```typescript
* type MyTuple = [string, number, boolean];
* type First = FirstElement<MyTuple>; // string
* ```
*/
export type FirstElement<T extends readonly any[]> = T[0];
/**
* Extracts the last element type of a tuple
* @template T - The tuple type
* @example
* ```typescript
* type MyTuple = [string, number, boolean];
* type Last = LastElement<MyTuple>; // boolean
* ```
*/
export type LastElement<T extends readonly any[]> = T extends [...any[], infer L] ? L : never;
/**
* Extracts all element types except the first
* @template T - The tuple type
* @example
* ```typescript
* type MyTuple = [string, number, boolean];
* type Rest = RestElements<MyTuple>; // [number, boolean]
* ```
*/
export type RestElements<T extends readonly any[]> = T extends [any, ...infer R] ? R : never;
/**
* Extracts all element types except the last
* @template T - The tuple type
* @example
* ```typescript
* type MyTuple = [string, number, boolean];
* type Init = InitElements<MyTuple>; // [string, number]
* ```
*/
export type InitElements<T extends readonly any[]> = T extends [...infer I, any] ? I : never;
/**
* Creates a tuple type by prepending an element
* @template T - The element to prepend
* @template U - The existing tuple
* @example
* ```typescript
* type Original = [number, boolean];
* type Prefixed = Prepend<string, Original>; // [string, number, boolean]
* ```
*/
export type Prepend<T, U extends readonly any[]> = [T, ...U];
/**
* Creates a tuple type by appending an element
* @template T - The existing tuple
* @template U - The element to append
* @example
* ```typescript
* type Original = [string, number];
* type Appended = Append<Original, boolean>; // [string, number, boolean]
* ```
*/
export type Append<T extends readonly any[], U> = [...T, U];
/**
* Creates a tuple type by inserting an element at a specific position
* @template T - The existing tuple
* @template U - The element to insert
* @template I - The insertion index
* @example
* ```typescript
* type Original = [string, boolean];
* type Inserted = Insert<Original, number, 1>; // [string, number, boolean]
* ```
*/
export type Insert<T extends readonly any[], U, I extends number> = T extends [...infer A, ...infer B] ? A['length'] extends I ? [...A, U, ...B] : never : never;
/**
* Creates a tuple type by removing an element at a specific position
* @template T - The existing tuple
* @template I - The index to remove
* @example
* ```typescript
* type Original = [string, number, boolean];
* type Removed = Remove<Original, 1>; // [string, boolean]
* ```
*/
export type Remove<T extends readonly any[], I extends number> = T extends [...infer A, any, ...infer B] ? A['length'] extends I ? [...A, ...B] : never : never;
/**
* Creates a tuple type by replacing an element at a specific position
* @template T - The existing tuple
* @template U - The new element
* @template I - The replacement index
* @example
* ```typescript
* type Original = [string, number, boolean];
* type Replaced = Replace<Original, string, 1>; // [string, string, boolean]
* ```
*/
export type Replace<T extends readonly any[], U, I extends number> = T extends [...infer A, any, ...infer B] ? A['length'] extends I ? [...A, U, ...B] : never : never;
/**
* Creates a tuple type with elements in reverse order
* @template T - The tuple type
* @example
* ```typescript
* type Original = [string, number, boolean];
* type Reversed = Reverse<Original>; // [boolean, number, string]
* ```
*/
export type Reverse<T extends readonly any[]> = T extends [infer F, ...infer R] ? [...Reverse<R>, F] : [];
/**
* Creates a tuple type by concatenating two tuples
* @template T - The first tuple
* @template U - The second tuple
* @example
* ```typescript
* type First = [string, number];
* type Second = [boolean, Date];
* type Concatenated = Concat<First, Second>; // [string, number, boolean, Date]
* ```
*/
export type Concat<T extends readonly any[], U extends readonly any[]> = [...T, ...U];
/**
* Creates a tuple type with a specific range of elements
* @template T - The tuple type
* @template Start - The start index (inclusive)
* @template End - The end index (exclusive)
* @example
* ```typescript
* type Original = [string, number, boolean, Date, symbol];
* type Sliced = Slice<Original, 1, 4>; // [number, boolean, Date]
* ```
*/
export type Slice<T extends readonly any[], Start extends number, End extends number> = T extends [...infer A, ...infer B] ? A['length'] extends Start ? B extends [...infer C, ...infer D] ? C['length'] extends End ? C : never : never : never : never;
export {};
//# sourceMappingURL=array.d.ts.map