//#region src/array.d.ts /** * Matches any array or array-like object. */ type Arrayable = T | T[]; type UnknownArray = readonly unknown[]; /** * Matches any [typed array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray), like `Uint8Array` or `Float64Array`. */ type TypedArray = BigInt64Array | BigUint64Array | Float32Array | Float64Array | Int8Array | Int16Array | Int32Array | Uint8Array | Uint8ClampedArray | Uint16Array | Uint32Array; /** * Infer the length of the given array ``. * * Check out {@link https://itnext.io/implementing-arithmetic-within-typescripts-type-system-a1ef140a6f6f | this article} for more information. */ type ArrayLength = T extends { readonly length: infer L; } ? L : 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. */ type ArrayElement = T extends readonly unknown[] ? T[0] : never; /** * Provides all values for a constant array or tuple. * * Use-case: This type is useful when working with constant arrays or tuples and you want to enforce type-safety with their values. * * @example * ``` * import type {ArrayValues, ArrayIndices} from 'type-fest'; * * const weekdays = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'] as const; * * type WeekdayName = ArrayValues; * type Weekday = ArrayIndices; * * const getWeekdayName = (day: Weekday): WeekdayName => weekdays[day]; * ``` * * @see {@link ArrayIndices} */ type ArrayValues = T[number]; /** * Provides valid indices for a constant array or tuple. * * Use-case: This type is useful when working with constant arrays or tuples and you want to enforce type-safety for accessing elements by their indices. * * @example * ``` * import type {ArrayIndices, ArrayValues} from 'type-fest'; * * const weekdays = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'] as const; * * type Weekday = ArrayIndices; * type WeekdayName = ArrayValues; * * const getWeekdayName = (day: Weekday): WeekdayName => weekdays[day]; * ``` * * @see {@link ArrayValues} */ type ArrayIndices = Exclude["length"], Element["length"]>; /** * Matches any unknown array or tuple. */ type UnknownArrayOrTuple = readonly [...unknown[]]; /** * Extracts the type of the first element of an array or tuple. */ type FirstArrayElement = TArray extends readonly [infer THead, ...unknown[]] ? THead : never; /** * Extracts the type of the last element of an array. * * Use-case: Defining the return type of functions that extract the last element of an array, for example [`lodash.last`](https://lodash.com/docs/4.17.15#last). * * @example * ``` * import type {LastArrayElement} from 'type-fest'; * * declare function lastOf(array: V): LastArrayElement; * * const array = ['foo', 2]; * * typeof lastOf(array); * //=> number * * const array = ['foo', 2] as const; * * typeof lastOf(array); * //=> 2 * ``` */ type LastArrayElement = Elements extends readonly [] ? ElementBeforeTailingSpreadElement : Elements extends readonly [...infer _U, infer V] ? V : Elements extends readonly [infer U, ...infer V] ? LastArrayElement : Elements extends readonly (infer U)[] ? ElementBeforeTailingSpreadElement | U : 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] * ``` */ type StaticPartOfArray = T extends unknown ? number extends T["length"] ? T extends readonly [infer U, ...infer V] ? StaticPartOfArray : Result : T : never; /** * 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[] * ``` */ type VariablePartOfArray = T extends unknown ? T extends readonly [...StaticPartOfArray, ...infer U] ? U : [] : never; //#endregion export { ArrayElement, ArrayIndices, ArrayLength, ArrayValues, Arrayable, FirstArrayElement, LastArrayElement, StaticPartOfArray, TypedArray, UnknownArray, UnknownArrayOrTuple, VariablePartOfArray }; //# sourceMappingURL=array.d.cts.map