Skip to content

Latest commit

 

History

History
1993 lines (1356 loc) · 32.3 KB

File metadata and controls

1993 lines (1356 loc) · 32.3 KB

typelab / utils

Handy set of TypeScript utility types for transforming and working with other types. Includes helpers for arrays, objects, unions, functions, and common type operations like extracting, excluding, mapping, and parsing.

Type Aliases

Type Alias Description

ArrayLength

Extracts the length of an Array type.

Example

type Length = ArrayLength<[1, 2, 3]>; // 3
type Length2 = ArrayLength<string[]>; // number
type Never = ArrayLength<any>; // never

ArrayElements

Extracts the elements of an Array type`.

Example

type Elements = ArrayElements<[string, number]>; // string | number
type Never = ArrayElements<any>; // never

ArrayValues

Alias for `ArrayElements`.

Extracts the values of an Array type`.

Example

type Values = ArrayValues<[string, number]>; // string | number
type Never = ArrayValues<any>; // never

ArrayIndexes

Extracts the index keys of an Array or Tuple type.

  • For a Tuple, returns T index union.
  • For a general Array, returns number since arrays are indexed by numbers.

Example

// 0 | 1 | 2
type Result1 = ArrayIndexes<[string, number, boolean]>;

// 0 | 1 | 2 | "0" | "1" | "2"
type Result2 = ArrayIndexes<[string, number, boolean], true>;

// number
type Result3 = ArrayIndexes<string[]>;

// never
type Never = ArrayIndexes<any>;

ArrayAt

Extracts the type of an element at a specific index from an Array type.

Example

type ElementAt = ArrayAt<[string, number, boolean], 1>; // number
type Undefined = ArrayAt<[string, number, boolean], 3>; // undefined
type Never = ArrayAt<any, 1>; // never

ArrayFirst

Extracts the type of the first element of an Array type.

Example

// string
type FirstElement = ArrayFirst<[string, number, boolean]>;

// string | number | boolean
type AllElements = ArrayFirst<(string | number | boolean)[]>;

// never
type Never = ArrayFirst<any>;

ArrayLast

Extracts the type of the last element of an Array type.

Example

// boolean
type LastElement = ArrayLast<[string, number, boolean]>;

// string | number | boolean
type AllElements = ArrayLast<(string | number | boolean)[]>;

// never
type Never = ArrayLast<any>;

ArrayConcat

Concatenates two Array types into one.

Template

The first Array type to concatenate.

Template

The second Array type to concatenate.

Example

// [string, number, boolean, object]
type Concatenated1 = ArrayConcat<[string, number], [boolean, object]>;

// [...string[], boolean, object]
type Concatenated2 = ArrayConcat<string[], [boolean, object]>;

// [string, number, ...object[]]
type Concatenated3 = ArrayConcat<[string, number], object[]>;

// [string, number, any]
type Concatenated4 = ArrayConcat<[string, number], any>;

// never
type Never = ArrayConcat<any, [string, number]>;

ArrayPush

Adds a new element to the end of a Writable Array type.

Example

type Pushed = ArrayPush<[string, number], boolean>; // [string, number, boolean]
type Same = ArrayPush<[string, number], never>; // [string, number]
type Never1 = ArrayPush<readonly [string, number], string> // never
type Never2 = ArrayPush<any, string> // never

ArrayPop

Removes the last element from an Writable Array type. If the Array is a Tuple, the result will be a new Tuple without the last element. If the Array is not a Tuple, the result will be the original Array type.

Example

type Popped = ArrayPop<[string, number]>; // [string]
type Same = ArrayPop<(string | number)[]>; // (string | number)[]
type Never1 = ArrayPop<readonly [string, number]>; // never
type Never2 = ArrayPop<any>; // never

ArrayUnshift

Adds a new element to the beginning of an Writable Array type.

Example

// [boolean, string, number]
type Unshifted = ArrayUnshift<[string, number], boolean>;

// [string, number]
type Same = ArrayUnshift<[string, number], never>;

// never
type Never1 = ArrayPush<readonly [string, number], string>;

// never
type Never2 = ArrayPush<any, string>;

ArrayShift

Removes the first element from an Writable Array type. If the Array is a Tuple, the result will be a new Tuple without the first element. If the Array is not a Tuple, the result will be the original Array type.

Example

type Shifted = ArrayShift<[string, number]>; // [number]
type Same = ArrayShift<(string | number)[]>; // (string | number)[]
type Never1 = ArrayShift<readonly [string, number]>; // never
type Never2 = ArrayShift<any>; // never

ArrayFlat

Type that flattens a nested Array structure into a single Array. If an element is an Array, it will recursively flatten it until no Array remains.

Example

// [0, 1, 2, 3, 4, 5, 6, 7]
type Flattened1 = ArrayFlat<[0, 1, [2, 3, [4, 5, [6, 7[]]]]]>;

// (0 | 1 | 2 | 3 | 4 | 5 | 6 | 7)[]
type Flattened2 = ArrayFlat<(0 | 1 | [2] | [3] | [[4]] | [[5]] | [[[6]]] | [[[7[]]]])[]>;

// never
type Never = ArrayFlat<any>;

ArrayTake

Takes the first TakeLength elements from an array (or tuple).

If TakeLength is negative, the elements are taken starting from the end of the array/tuple.

Example

type Result1 = ArrayTake<[1, 2, 3, 4], 2>; // [1, 2]
type Result2 = ArrayTake<[1, 2, 3, 4], -2>; // [3, 4]
type Result3 = ArrayTake<[1, 2, 3, 4], 5>; // [1, 2, 3, 4]
type Result4 = ArrayTake<[1, 2, 3, 4]>; // [1, 2, 3, 4]
type Never1 = ArrayTake<[1, 2, 3, 4], any>; // never
type Never2 = ArrayTake<[1, 2, 3, 4], never>; // never
type Never3 = ArrayTake<any, 1>; // never

ArraySkip

Skips the first SkipLength elements from an array (or tuple).

If SkipLength is negative, the elements are skipped starting from the end of the array/tuple.

Example

type Result1 = ArraySkip<[1, 2, 3, 4], 2>; // [3, 4]
type Result2 = ArraySkip<[1, 2, 3, 4], -2>; // [1, 2]
type Result3 = ArraySkip<[1, 2, 3, 4], 5>; // []
type Result4 = ArraySkip<[1, 2, 3, 4]>; // [1, 2, 3, 4]
type Never1 = ArraySkip<[1, 2, 3, 4], any>; // never
type Never2 = ArraySkip<any, 1>; // never

ArrayRequired

Extended TypeScript `Required` to handle Array.

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

ArrayPartial

Extended TypeScript `Partial` to handle Array.

Example

type Result1 = ArrayPartial<[string, string]>; // [string?, string?]
type Result2 = ArrayPartial<[string, string], 1>; // [string, string?]
type Never1 = ArrayPartial<[string, string], any>; // never
type Never2 = ArrayPartial<any, 1>; // never

ArrayRequiredIndexes

Get the required indexes from T type.

Example

// 0 | 1
type Result1 = ArrayRequiredIndexes<[string, string, string?]>;

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

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

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

// never
type Never2 = ArrayRequiredIndexes<any>;

ArrayOptionalIndexes

Get the optional indexes from T type.

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>;

ArrayRequiredElements

Get the required elements from T type.

Example

// 0 | 1
type Result1 = ArrayRequiredElements<[0, 1, 2?]>;

// string | undefined
type Result2 = ArrayRequiredElements<(string | undefined)[]>;

// never
type Never1 = ArrayRequiredElements<[0?, 1?, 2?]>;

// never
type Never2 = ArrayRequiredElements<any>;

ArrayOptionalElements

Get the optional elements from T type.

Example

// 1 | 2 | undefined
type Result1 = ArrayOptionalElements<[0, 1?, 2?]>;

// string | undefined
type Result2 = ArrayOptionalElements<(string | undefined)[]>;

// never
type Never1 = ArrayOptionalElements<[0, 1, 2]>;

// never
type Never2 = ArrayOptionalElements<any>;

ArrayRequiredValues

Alias for `ArrayRequiredElements`.

ArrayOptionalValues

Alias for `ArrayOptionalElements`.

ArrayOmitRequired

Omit the required elements from T type.

Example

// [string?, string?]
type Result1 = ArrayOmitRequired<[string, string, string?, string?]>;

// [unknown?, string?, string?]
type Result2 = ArrayOmitRequired<[string, string?, string?], false>;

// never
type Never = ArrayOmitRequired<any>;

ArrayOmitOptional

Omit the optional elements from T type.

Example

// [string, string]
type Result = ArrayOmitOptional<[string, string, string?]>;

// never
type Never = ArrayOmitOptional<any>;

ArrayToObject

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.

Example

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

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

// never
type Never = ArrayToObject<any>;

ArrayAssign

Assign the elements of Source into Target.

It's like ObjectAssign but for Array only.

  • If Target is readonly, it returns Target as is. In order to follow the actual result of Object.assign.
  • If either Target or Source is not an Array or Tuple, it returns Target as is.

Example

// [0, 1, 2]
type Result1 = ArrayAssign<[1, 2], [0, 1, 2]>;

// (string | number)[]
type Result2 = ArrayAssign<string[], number[]>;

// readonly [1, 2]
type Result3 = ArrayAssign<readonly [1, 2], [0, 1, 2]>;

ArrayOverwrite

Overwrite elements of Target with elements of Source.

This type will overwrite all nested Array types (if Z is 'deep').

Example

type Arr1 = [string, [string, string]];
type Arr2 = [number, [number], number];

// [number, [number], number]
type Shallow = ArrayOverwrite<Arr1, Arr2, 'shallow'>;

// [number, [number, string], number]
type Deep = ArrayOverwrite<Arr1, Arr2, 'deep'>;

ArrayMerge

Merges elements of Target with elements of Source.

This type will merge all nested Array types (if Z is 'deep').

Example

type Arr1 = [string, [string, string]];
type Arr2 = [number, [number], number];

// [string | number, [number] | [string, string], number]
type Shallow = ArrayMerge<Arr1, Arr2, 'shallow'>;

// [string | number, [string | number, string], number]
type Deep = ArrayMerge<Arr1, Arr2, 'deep'>;

ArrayUnionize

Unions the elements of an Array with a specified UnionType, creating a new Array where each element is either the original element or the UnionType.

This type will unionize all nested Array types (if Z is 'deep').

Example

// [string | number, number | [number, boolean]]
type Shallow = ArrayUnionize<[string, [number, boolean]], number, 'shallow'>;

// [string | number, [number, number | boolean]]
type Deep = ArrayUnionize<[string, [number, boolean]], number, 'deep'>;

Brand

Create a branded version of a base type.

Example

type UserId = Brand<string, 'UserId'>; // string & { __brand: "UserId" }
type OrderId = Brand<string, 'OrderId'>; // string & { __brand: "OrderId" }
type IsEqual = [UserId, OrderId] extends [OrderId, UserId] ? true : false; // false

Detailed

Recursively applies a deep transformation to each property of T, only if T is ObjectLiteral.

Example

type ComplexObj = Required<{ b?: string } & { c?: string }> & { d: string };
type Obj = { a: string; nested: ComplexObj };

// Detail is { a: string; nested: { b: string; c: string; d: string }; }
type Detail = Detailed<Obj>;

ExcludeNullish

Exclude null and undefined from type T.

Example

type NonNullishType = ExcludeNullish<string | null | undefined>; // string

ExcludeNull

Exclude null from type T.

Example

type NonNullType = ExcludeNull<string | null>; // string

ExcludeUndefined

Exclude undefined from type T.

Example

type NonUndefinedType = ExcludeUndefined<string | undefined>; // string

ExtractArray

Extract the value type from an Array type.

Example

type Valid = ExtractArray<number[]>; // number
type Invalid = ExtractArray<Promise<number>> // never

ExtractPromise

Extract the value type from a Promise type.

Example

type Valid = ExtractPromise<Promise<number>>; // number
type Invalid = ExtractPromise<number>> // never

ExtractParams

Type that extracts the parameters from Function, Newable Function, or the constructor parameters of a Class type.

Example

// [string, number]
type FunctionParams = ExtractParams<(a: string, b: number) => void>;

// [string, number]
type NewableParams = ExtractParams<new (a: string, b: number) => { a: string; b: string }>;

class MyClass { constructor(public a: string, public b: number) {}}
// [string, number]
type ClassParams = ExtractParams<typeof MyClass>;

ExtractReturn

Type that extracts the return type from Function, Newable Function, or the instance type of a Class type.

Example

// string
type FunctionReturn = ExtractReturn<(a: string, b: number) => string>;

// { a: string; b: string }
type NewableReturn = ExtractReturn<new (a: string, b: number) => { a: string; b: string }>;

FunctionAssign

Assign the parameter and return types of Source into Target.

Use `ArrayAssign` for the parameter type and `ObjectAssign` for the return type.

  • If Target or Source is not Function, it returns never.

Example

// (param_0: number, param_1: string) => { a: number; b: string }
type Result = FunctionAssign<(...param: [string, string]) => { a: string; b: string }, (...param: [number]) => { a: number }>

FnAssign

Alias for `FunctionAssign`.

FunctionOverwrite

Overwrite the parameter and return types of Target with parameter and return types of Source.

Use `ArrayOverwrite` for the parameter type and `ObjectOverwrite` for the return type.

  • If Target or Source is not Function, it returns never.

Example

type Obj1 = { a: string; b: { a: string } };
type Obj2 = { a: number; b: { b: number } };

// (param_0: number, param_1: string) => { a: number; b: { b: number; }; }
type Shallow = FunctionOverwrite<(...param: [string, string]) => Obj1, (...param: [number]) => Obj2, 'shallow'>;

// (param_0: number, param_1: string) => { a: number; b: { a: string, b: number; }; }
type Deep = FunctionOverwrite<(...param: [string, string]) => Obj1, (...param: [number]) => Obj2, 'deep'>;

FnOverwrite

Alias for `FunctionOverwrite`.

FunctionMerge

Merges the parameter and return types of Source into Target.

Use `ArrayMerge` for the parameter type and `ObjectMerge` for the return type.

  • If Target or Source is not Function, it returns never.

Example

type Obj1 = { a: string; b: { a: string } };
type Obj2 = { a: number; b: { b: number } };

// (param_0: string | number, param_1: string) => { a: string | number; b: { a: string; } | { b: number; }; }
type Shallow = FunctionMerge<(...param: [string, string]) => Obj1, (...param: [number]) => Obj2, 'shallow'>;

// (param_0: string | number, param_1: string) => { a: string | number; b: { a: string; b: number; }; }
type Deep = FunctionMerge<(...param: [string, string]) => Obj1, (...param: [number]) => Obj2, 'deep'>;

FnMerge

Alias for `FunctionMerge`.

FunctionPromisify

Converts a Function type that returns a Promise instead of the original return type.

If the Function takes a callback (e.g., (error, result) => void), it converts the Function to return a Promise with the callback result. Otherwise, it wraps the return type in a Promise.

Example

// Callback-style function:
// (a: string) => Promise<string>
type Promisified = FunctionPromisify<(a: string, callback: (error: Error, result: string) => void) => void>;

// Regular function:
// (a: string) => Promise<string>
type PromisifiedFn = FunctionPromisify<(a: string) => string>;

FnPromisify

Alias for `FunctionPromisify`.

FunctionCallbackify

Converts an Async Function type into a Callback-style Function.

Example

// (a: string, callback: (error: unknown, result: string) => void) => void
type Callbackified = FunctionCallbackify<(a: string) => Promise<string>>;

FnCallbackify

Alias for `FunctionCallbackify`.

KeyOf

Extracts the keys of an object type.

Example

// Keys is 'a' | 'b'
type Keys = KeyOf<{ a: string; b: number }>;

Mapped

Recursively applies a shallow transformation to each property of T.

Simpler version of `Detailed`

Example

type Obj = Required<{ b?: string } & { c?: string }> & { d: string };

// Detail is { b: string; c: string; d: string }
type Detail = Mapped<Obj>;

Increment

Increments a given number or bigint by 1.

Example

type IncrementNumber = Increment<5>; // 6
type IncrementBigInt = Increment<10n>; // 11n
type IncrementNegative = Increment<-3.14>; // -2.14

Decrement

Decrements a given number or bigint by 1.

Example

type DecrementNumber = Decrement<5>; // 4
type DecrementBigInt = Decrement<10n>; // 9n
type DecrementNegative = Decrement<-3.14>; // -4.14

ObjectPick

Extended TypeScript `Pick` to pick K from T.

Example

type Obj = { a: string; b: string };

// { a: string; }
type Picked = ObjectPick<Obj, 'a'>;

ObjectRequired

Extended TypeScript `Required` to enforce specific properties as required.

Example

type Obj = { a?: string; b?: string };

// { a: string; b?: string }
type RequiredObj = ObjectRequired<Obj, 'a'>;

ObjectPartial

Extended TypeScript `Partial` to make specific properties optional.

Example

type Obj = { a: string; b: string };

// { a?: string; b: string }
type PartialObj = ObjectPartial<Obj, 'a'>;

ObjectRequiredKeys

Get the required keys from T type.

Example

type Obj = { a: number; b: string | undefined; c?: boolean };
type RequiredKeys = ObjectRequiredKeys<Obj>; // "a" | "b"

ObjectOptionalKeys

Get the optional keys from T type.

Example

type Obj = { a: number; b: string | undefined; c?: boolean };
type OptionalKeys = ObjectOptionalKeys<Obj>; // "c"

ObjectOmitRequired

Get the optional properties from T type.

This type will lookup all nested objects (if Z is 'deep').

Example

type Obj = { a: string | undefined; b?: string; c?: { d: string; e?: string } };

// { b?: string; c?: { d: string; e?: string; }; }
type Shallow = ObjectOmitRequired<Obj, 'shallow'>;

// { b?: string; c?: { e?: string; }; }
type Deep = ObjectOmitRequired<Obj, 'deep'>;

ObjectOmitOptional

Get the required properties from T type.

This type will lookup all nested objects (if Z is 'deep').

Example

type Obj = { a: string | undefined; b?: string; c: { d: string, e?: string } };

// { a: string | undefined; c: { d: string; e?: string } }
type Shallow = ObjectOmitOptional<Obj, 'shallow'>;

// { a: string | undefined; c: { d: string; }}
type Deep = ObjectOmitOptional<Obj, 'deep'>;

ObjectReadonly

Extended TypeScript `Readonly` to enforce specific properties as readonly.

Example

type Obj = { a: string; b: string };

// { readonly a: string; b: string }
type ReadonlyObj = ObjectReadonly<Obj, 'a'>;

ObjectWritable

Enforce specific properties as writable.

Example

type Obj = { readonly a: string; b: string };

// WritableObj is { a: string; b: string; }
type WritableObj = ObjectWritable<Obj, 'a'>;

ObjectPath

Get all of the paths from T as a string union.

Example

type Obj = { a: { b: number }; c: string };
type Paths = ObjectPath<Obj>; // "a" | "a.b" | "c"

ObjectPathValue

Get the type of a property based on a given path.

Example

type Obj = { a: { b: number }; c: string };
type ValueType = ObjectPathValue<Obj, 'a.b'>; // number

ObjectToTuple

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.

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>;

ObjectAssign

Assign properties of Source into Target.

The result of type follows the actual result of Object.assign.

Example

// { a: string; b: string; c: boolean }
type Assign1 = ObjectAssign<{ a: string; b: number }, { b: string; c: boolean }>;

// { a: string; 0: string }
type Assign2 = ObjectAssign<{ a: string }, [string]>;

// { [x: number]: string, a: string }
type Assign3 = ObjectAssign<{ a: string }, string[]>;

// [number, number]
type Assign4 = ObjectAssign<[string], [number, number]>;

// (string | number)[]
type Assign5 = ObjectAssign<string[], [number, number]>;

// (string | number)[]
type Assign6 = ObjectAssign<string[], number[]>;

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

// (string | number)[]
type Assign8 = ObjectAssign<[number], 'str'>;

// { [x: number]: string, a: string }
type Assign9 = ObjectAssign<{a: string}, 'str'>;

ObjectOverwrite

Overwrite properties of Target with properties of Source.

This type will overwrite all nested object types (if Z is 'deep').

Example

type Obj1 = { a: string; b: string; c: { a: string } };
type Obj2 = { b: number; c: { b: number }; d: number };

// { a: string; b: number; c: { b: number; }; d: number }
type Shallow = ObjectOverwrite<Obj1, Obj2, 'shallow'>;

// { a: string; b: number; c: { a: string; b: number; }; d: number }
type Deep = ObjectOverwrite<Obj1, Obj2, 'deep'>;

ObjectMerge

Merges properties of Target and Source.

This type will merge all nested object types (if Z is 'deep').

Example

type Obj1 = { a: string; b: string; c: { a: string; } };
type Obj2 = { b: number; c: { a: number; b: number }; d: number };

// { a: string; b: string | number; c: { a: string; } | { a: number; b: number }; d: number }
type Shallow = ObjectMerge<Obj1, Obj2, 'shallow'>;

// { a: string; b: string | number; c: { a: string | number; b: number; }; d: number }
type Deep = ObjectMerge<Obj1, Obj2, 'deep'>;

ObjectUnionize

Unions the properties of an object with a specified UnionType, creating a new object where each property is either the original value or the UnionType.

This type will unionize all nested object types (if Z is 'deep').

Example

// { a: string | number; b: number | { a: string; }; }
type Shallow = ObjectUnionize<{ a: string; b: { a: string } }, number, 'shallow'>;

// { a: string | number; b: { a: string | number; }; }
type Deep = ObjectUnionize<{ a: string; b: { a: string } }, number, 'deep'>;

ParseInt

Parses a string or number type into a number.

Example

type Int = ParseInt<'11'>; // 11

ParseIntNegative

Parses a string or number type into a negative number.

Example

type Int = ParseIntNegative<'11'>; // -11

ParseIntPositive

Parses a string or number type into a positive number.

Example

type Int = ParseIntPositive<'-11'>; // 11

ParseFloat

Parses a string or number type into a number.

Example

type Float = ParseFloat<'11.1'>; // 11.1

ParseFloatNegative

Parses a string or number type into a negative number.

Example

type Float = ParseFloatNegative<'11.1'>; // -11.1

ParseFloatPositive

Parses a string or number type into a positive number.

Example

type Float = ParseFloatPositive<'-11.1'>; // 11.1

ParseBigInt

Parses a string or number type into a bigint.

Example

type Bigi = ParseBigInt<'11n'>; // 11n

ParseBigIntNegative

Parses a string or number type into a negative bigint.

Example

type Bigi = ParseBigIntNegative<'11'>; // -11n

ParseBigIntPositive

Parses a string or number type into a positive bigint.

Example

type Bigi = ParseBigIntPositive<'-11'>; // 11n

ParseString

Converts a type to a string.

Example

type Stringified = ParseString<11>; // '11'

ParseBoolean

Converts a type to a boolean.

Example

type True1 = ParseBoolean<'true'>; // true
type True2 = ParseBoolean<1>; // true
type False1 = ParseBoolean<'false'>; // false
type False2 = ParseBoolean<0>; // false

ParseObject

Converts a type to an object.

Example

type Obj = ParseObject<true>; // { valueOf: () => boolean }

PropValue

Gets the type of a property K from T.

Example

type User = { name: string };
type Valid = PropValue<User, 'name'>; // string
type Invalid = PropValue<User, 'email'>; // never

Toggle

Toggle the values of a boolean type.

Example

type Toggled1 = Toggle<true>; // false
type Toggled2 = Toggle<false>; // true

UnionToIntersection

Converts a union type to an intersection type.

Example

// { a: number } & { b: string }
type Intersect = UnionToIntersection<{ a: number } | { b: string }>;

UnionLast

Extracts the last type from a union type.

⚠️ Does not guarantee correct order.

Example

// 'c'
type Last = UnionLast<'a' | 'b' | 'c'>;

UnionPop

Removes the last type from a union.

⚠️ Does not guarantee correct order.

Example

// 'a' | 'b'
type Popped = UnionPop<'a' | 'b' | 'c'>;

UnionIntersection

Get the intersection of two union types, combining only the types that exist in both unions.

Example

// 'b'
type Result = UnionIntersection<'a' | 'b', 'b' | 'c'>;

UnionExclusive

Creates a union of types that are exclusive to each other.

Example

// 'a' | 'c'
type Exclusive = UnionExclusive<'a' | 'b', 'b' | 'c'>;

UnionToTuple

Converts a union type to a Tuple.

⚠️ Does not guarantee correct order.

Example

// ['a', 'b', 'c']
type Tupl = UnionToTuple<'a' | 'b' | 'c'>;

ValueOf

Extracts the values of an object type.

Example

// Values is string | number
type Values = ValueOf<{ a: string; b: number }>;