Developer Tools

General Utils

General utilities and helpers for composing, manipulating, and validating types in TypeScript. Includes helpers for improving readability, creating literal unions, excluding types, working with primitive values, and more.

@halvaradop/ts-utility-types/utils

General utilities and helpers for composing, manipulating, and validating types in TypeScript. Includes helpers for improving readability, creating literal unions, excluding types, working with primitive values, and more.

Installation

npm i -D @halvaradop/ts-utility-types

Usage

import type { , ,  } from "@halvaradop/ts-utility-types/utils"
import type * as  from "@halvaradop/ts-utility-types/utils" 


type  = .
  • ArgsFunction
  • Discard
  • Falsy
  • LiteralUnion
  • Nullish
  • Prettify
  • Primitive
  • PrimitiveNullish
  • ReturnTypeOf
  • WhiteSpaces

Prettify<Obj, object>

Improves the readability of an object type by expanding its properties on separate lines. Does not modify the original type.

ArgumentTypeDescription
ObjobjectThe object type to prettify.
import type {  } from "@halvaradop/ts-utility-types/utils"

type Pretty = <{ : string; : number }>
type Pretty = {
    foo: string;
    bar: number;
}

LiteralUnion<T, U = string>

Extends the values of a literal type with a broader type (defaults to string).

ArgumentTypeDescription
TUThe literal type(s) to extend.
UstringThe base type to extend with (default string).
import type {  } from "@halvaradop/ts-utility-types/utils"

type ExtendedFoo = <"foo" | "bar">
type ExtendedFoo = "foo" | "bar" | (string & Record<never, never>)

Discard<Type, Extends, Value = never, Reverse = false>

Includes or excludes types based on assignability to another type. If Reverse is true, includes matching types; if false, excludes them.

ArgumentTypeDescription
TypeanyThe union type to filter.
ExtendsanyThe type to check assignability against.
ValueanyThe value to use for excluded/included types (default never).
ReversebooleanIf true, includes matching types; if false, excludes them (default false).
import type {  } from "@halvaradop/ts-utility-types/utils"

type A = <string | number, string>
type A = number
type B = <string | number, boolean>
type B = string | number

ArgsFunction

Represents a function that accepts any number of arguments of any type.

import type {  } from "@halvaradop/ts-utility-types/utils"

const fn:  = (...) => {}
const fn: ArgsFunction

Nullish

Represents the absence of value: null or undefined.

import type {  } from "@halvaradop/ts-utility-types/utils"

type CanBeNull = string | 
type CanBeNull = string | Nullish

PrimitiveNullish

A primitive type that can also be null or undefined.

import type {  } from "@halvaradop/ts-utility-types/utils"

type PrimitiveOrNull = 
type PrimitiveOrNull = string | number | bigint | boolean | symbol | Nullish

Primitive

A primitive type: number, string, boolean, bigint, or symbol (excludes nullish).

import type {  } from "@halvaradop/ts-utility-types/utils"

type PrimitiveValues = 
type PrimitiveValues = {}

WhiteSpaces

Represents whitespace characters recognized by TypeScript.

import type {  } from "@halvaradop/ts-utility-types/utils"

type Spaces = 
type Spaces = " " | "\n" | "\t" | "\r" | "\f" | "\v" | " " | " " | " " | " " | " " | " " | " " | " " | " " | " " | " " | " " | " " | "\u2028" | "\u2029" | " " | " " | " "

Falsy

Represents falsy values: null, undefined, 0, false, or "".

import type {  } from "@halvaradop/ts-utility-types/utils"

type FalsyValue = 
type FalsyValue = false | "" | 0 | Nullish

ReturnTypeOf<T>

Determines the primitive type corresponding to the provided value.

ArgumentTypeDescription
TanyThe value to get the primitive type of.
import type {  } from "@halvaradop/ts-utility-types/utils"

type TypeOfValue = <123>
type TypeOfValue = number
type TypeOfValueString = <"hello">
type TypeOfValueString = string
type TypeOfValueBool = <true>
type TypeOfValueBool = boolean
type TypeOfValueArr = <[1, 2, 3]>
type TypeOfValueArr = [1, 2, 3]