import type {IfNever} from '../if-never'; import type {UnknownArray} from '../unknown-array'; /** Infer the length of the given array ``. @link https://itnext.io/implementing-arithmetic-within-typescripts-type-system-a1ef140a6f6f */ type ArrayLength = T extends {readonly length: infer L} ? L : never; /** Matches any unknown array or tuple. */ export type UnknownArrayOrTuple = readonly [...unknown[]]; // TODO: should unknown-array be updated? /** Extracts the type of the first element of an array or tuple. */ export type FirstArrayElement = TArray extends readonly [infer THead, ...unknown[]] ? THead : never; /** Extract the element of an array that also works for array union. Returns `never` if T is not an array. It creates a type-safe way to access the element type of `unknown` type. */ export type ArrayElement = T extends readonly unknown[] ? T[0] : never; /** Returns the static, fixed-length portion of the given array, excluding variable-length parts. @example ``` type A = [string, number, boolean, ...string[]]; type B = StaticPartOfArray; //=> [string, number, boolean] ``` */ export type StaticPartOfArray = T extends unknown ? number extends T['length'] ? T extends readonly [infer U, ...infer V] ? StaticPartOfArray : Result : T : never; // Should never happen /** Returns the variable, non-fixed-length portion of the given array, excluding static-length parts. @example ``` type A = [string, number, boolean, ...string[]]; type B = VariablePartOfArray; //=> string[] ``` */ export type VariablePartOfArray = T extends unknown ? T extends readonly [...StaticPartOfArray, ...infer U] ? U : [] : never; // Should never happen /** Set the given array to readonly if `IsReadonly` is `true`, otherwise set the given array to normal, then return the result. @example ``` type ReadonlyArray = readonly string[]; type NormalArray = string[]; type ReadonlyResult = SetArrayAccess; //=> readonly string[] type NormalResult = SetArrayAccess; //=> string[] ``` */ export type SetArrayAccess = T extends readonly [...infer U] ? IsReadonly extends true ? readonly [...U] : [...U] : T; /** Returns whether the given array `T` is readonly. */ export type IsArrayReadonly = IfNever; /** An if-else-like type that resolves depending on whether the given array is readonly. @see {@link IsArrayReadonly} @example ``` import type {ArrayTail} from 'type-fest'; type ReadonlyPreservingArrayTail = ArrayTail extends infer Tail ? IfArrayReadonly, Tail> : never; type ReadonlyTail = ReadonlyPreservingArrayTail; //=> readonly [number, boolean] type NonReadonlyTail = ReadonlyPreservingArrayTail<[string, number, boolean]>; //=> [number, boolean] type ShouldBeTrue = IfArrayReadonly; //=> true type ShouldBeBar = IfArrayReadonly; //=> 'bar' ``` */ export type IfArrayReadonly = IsArrayReadonly extends infer Result ? Result extends true ? TypeIfArrayReadonly : TypeIfNotArrayReadonly : never; // Should never happen