Skip to content
Zach Vaughan edited this page May 22, 2025 · 13 revisions

General-Purpose TypeScript Helper Types.


BaseType

type BaseType <TypeStringT extends BaseTypeString = never> = string | number | bigint | boolean | symbol | object | any[] | ((...args: any[]) => any) | null | undefined;

The Base Types from which all types are derived.

These types are a superset of the types recognized by the typeof operator.

The string-based variant of this type is BaseTypeString.

Template Parameters

  • TypeStringT: An optional BaseTypeString or union of BaseTypeString members used to determine which Base Types should be returned.

If omitted or never, a union of all of the Base Types will be returned.

Returns

A union of Base Types.

If TypeStringT contains a BaseTypeString or union of BaseTypeString members, a union containing all of the corresponding Base Types will be returned.

If TypeStringT is never or the entire BaseTypeString union, a union of all of the Base Types will be returned.


BaseTypeString

type BaseTypeString <TypeT extends BaseType = any> = "string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function" | "null" | "array";

The string values corresponding to each of the Base Types from which all types are derived.

This is the string-based variant of the BaseType type.

Template Parameters

  • TypeT: An optional BaseType or union of BaseType members used to determine which Base Types should be returned.

If omitted or any, a union of all of the Base Type string members will be returned.

Returns

A union of Base Type string members.

If TypeT contains a BaseType or union of BaseType members, a union containing all of the corresponding Base Type string members will be returned.

If TypeT is never or the entire BaseType union, a union of all of the Base Type string members will be returned.


getBaseType

function getBaseType <T extends BaseType> ( x: T ): BaseTypeString<T>;
// function getBaseType ( x: BaseType ): BaseTypeString;

Get the BaseType of the given value.

Template Parameters

  • T: The type of the specified value.

Function Parameters

  • x: The value being evaluated.

Returns

A BaseTypeString corresponding to the BaseType of x.


isBaseType

function isBaseType <TypesT extends BaseTypeString | BaseTypeString[]> (
    x: BaseType,
    types: TypesT
): x is BaseType<TypesT extends BaseTypeString[] ? TypesT[number] : TypesT>;
// function getBaseType ( x: BaseType, types: BaseTypeString | BaseTypeString[] ): boolean;

Check if the given value is of any of the specified BaseTypes.

Template Parameters

  • TypesT: The type of the types argument.

Function Parameters

  • x: The value being evaluated.
  • types: The BaseTypeString or an array of BaseTypeStrings corresponding to the BaseType(s) to check for.

Returns

true if x is of the specified BaseType(s) or false if it is not.


assertBaseType

function assertBaseType <TypesT extends BaseTypeString | BaseTypeString[]> (
    x: BaseType,
    types: TypesT
): x is BaseType<TypesT extends BaseTypeString[] ? TypesT[number] : TypesT>;
// function assertBaseType ( x: BaseType, types: BaseTypeString | BaseTypeString[] ): true;

Assert the given value is of any of the specified BaseTypes and throw a TypeError if it is not.

Template Parameters

  • TypesT: The type of the types argument.

Function Parameters

  • x: The value being evaluated.
  • types: The BaseTypeString or an array of BaseTypeStrings corresponding to the BaseType(s) to check for.

Returns

true if x is of the specified BaseType(s).

Throws

A TypeError if x is not of the specified BaseType(s).


IsAny

type IsAny <T> = boolean;

Determine if the given type is any.

Template Parameters

  • T: The type being evaluated.

Returns

true if T is any and false otherwise.


UnionToIntersection

type UnionToIntersection <U> = any;

Transform the specified union into an intersection.

Template Parameters

  • U: The union being transformed into an intersection.

Returns

An intersection of the union members of U.

If U is not a union or is a generic type such as any, returns U.

If the intersection of the union members of U produces an impossible combination (e.g., true & false), returns never.