Skip to content

Latest commit

 

History

History
110 lines (75 loc) · 1.59 KB

File metadata and controls

110 lines (75 loc) · 1.59 KB

typelab / utils / ObjectToTuple

type ObjectToTuple<T, IncludeNonIndex, HoleType, OmitHole> = IsArray<T> extends true ? T : IsObjectLiteral<T> extends true ? _ObjectToTuple<T, IncludeNonIndex, HoleType, OmitHole> : never;

Converts an object into a Tuple of its values.

  • If IncludeNonIndex is false, only properties with numeric keys are included.
  • If IncludeNonIndex is true, all properties are included.

⚠️ Does not guarantee correct order, if IncludeNonIndex is true.

Type Parameters

Type Parameter Default type Description

T

The object type to convert.

IncludeNonIndex extends boolean

false

A flag that determines whether to include non-array index or not. defaults to false.

HoleType

unknown

Safety type, if there is a hole(s) in result, defaults to unknown. Ignored if OmitHole is true.

OmitHole extends boolean

false

Each element with never type will be omitted, defaults to false.

Returns

A Tuple type, if T is ObjectLiteral or Tuple, never otherwise.

Example

type Obj1 = { 0: string; 1: number; 2: boolean };
// [string, number, boolean]
type TupleType1 = ObjectToTuple<Obj1>;

type Obj2 = { a: 'a'; b: 'b'; c: 'c' };
// ['a', 'b, 'c']
type TupleType2 = ObjectToTuple<Obj2, true>;