Skip to content

Latest commit

 

History

History
52 lines (37 loc) · 872 Bytes

File metadata and controls

52 lines (37 loc) · 872 Bytes

typelab / utils / ArrayToObject

type ArrayToObject<T> = T extends ReadonlyArray ? _IfNotAnyOrNever<T, _ArrayToObject<T>> : never;

Converts an Array or Tuple type into an object type with numeric keys.

  • For a Tuple, keys are the Tuple indices and values are the corresponding elements.
  • For an Array, the keys are numbers and values are the Array elements.

Type Parameters

Type Parameter Description

T

The Array or Tuple type to convert.

Returns

An object type where:

Example

// { 0: string, 1: number }
type Result1 = ArrayToObject<[string, number]>;

// { [x: number]: string }
type Result2 = ArrayToObject<string[]>;

// never
type Never = ArrayToObject<any>;