Skip to content

Latest commit

 

History

History
86 lines (60 loc) · 1.28 KB

File metadata and controls

86 lines (60 loc) · 1.28 KB

typelab / utils / ArrayRequired

type ArrayRequired<T, Index, IncludeUndefined> = T extends ReadonlyArray<infer Element> ? _IfNotAnyOrNever<T, _IfNotAnyOrNever<Index, _ArrayRequired<T, Index, Element, IncludeUndefined>>> : never;

Extended TypeScript `Required` to handle Array.

Type Parameters

Type Parameter Default type Description

T

The original Array type.

Index

ArrayIndexes<T, true>

The index of T that should be required.

IncludeUndefined extends boolean

false

Include undefined type from T[Index], defaults to false.

Returns

An Array type with the specified K set as required.

Example

type Result1 = ArrayRequired<[string?, string?]>; // [string, string]
type Result2 = ArrayRequired<[string?, string?], 0>; // [string, string?]
type Result3 = ArrayRequired<[string?, string?], 0, true>; // [string | undefined, string?]
type Never1 = ArrayRequired<[string?, string?], any>; // never
type Never2 = ArrayRequired<any, 1>; // never