UNPKG

725 BTypeScriptView Raw
1/**
2Provides valid indices for a constant array or tuple.
3
4Use-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.
5
6@example
7```
8import type {ArrayIndices, ArrayValues} from 'type-fest';
9
10const weekdays = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'] as const;
11
12type Weekday = ArrayIndices<typeof weekdays>;
13type WeekdayName = ArrayValues<typeof weekdays>;
14
15const getWeekdayName = (day: Weekday): WeekdayName => weekdays[day];
16```
17
18@see {@link ArrayValues}
19
20@category Array
21*/
22export type ArrayIndices<Element extends readonly unknown[]> =
23 Exclude<Partial<Element>['length'], Element['length']>;