Skip to content

Latest commit

 

History

History
78 lines (54 loc) · 1.29 KB

File metadata and controls

78 lines (54 loc) · 1.29 KB

typelab / utils / ArrayOptionalIndexes

type ArrayOptionalIndexes<T, IncludeString> = T extends ReadonlyArray ? _IfNotAnyOrNever<T, _ArrayOptionalIndexes<T> extends infer IndexesNumber ? number extends IndexesNumber ? IndexesNumber : IsTrue<IncludeString> extends true ? IndexesNumber | ParseString<IndexesNumber> : IndexesNumber : never> : never;

Get the optional indexes from T type.

Type Parameters

Type Parameter Default type Description

T

The Array type to extract optional indexes from.

IncludeString extends boolean

false

Whether to include string keys (true) or only numeric keys (false). Defaults to false.

Returns

The indexes of T that are optional.

Example

// 1 | 2
type Result1 = ArrayOptionalIndexes<[string, string?, string?]>;

// 1 | 2 | "1" | "2"
type Result2 = ArrayOptionalIndexes<[string, string?, string?], true>;

// number
type Result3 = ArrayOptionalIndexes<(string | undefined)[]>;

// never
type Never1 = ArrayOptionalIndexes<[string, string, string]>;

// never
type Never2 = ArrayOptionalIndexes<any>;