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.
import type * as Utils from "@halvaradop/ts-utility-types/utils"
Prettify<Obj extends object>
Improves the readability of an object type by expanding its properties on separate lines. Does not modify the original type.
// Expected: { foo: string; bar: number }
type Pretty = Prettify<{ foo: string; bar: number }>
LiteralUnion<T extends U, U = string>
Extends the values of a literal type with a broader type (defaults to string
).
// Expected: "foo" | "bar" | string
type ExtendedFoo = LiteralUnion<"foo" | "bar">
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.
// Expected: number
type A = Discard<string | number, string>
// Expected: string | number
type B = Discard<string | number, boolean>
ArgsFunction
Represents a function that accepts any number of arguments of any type.
const fn: ArgsFunction = (...args) => {}
Nullish
Represents the absence of value: null
or undefined
.
type CanBeNull = string | Nullish
PrimitiveNullish
A primitive type that can also be null
or undefined
.
type PrimitiveOrNull = PrimitiveNullish
Primitive
A primitive type: number
, string
, boolean
, bigint
, or symbol
(excludes nullish).
type Primitive = Primitive
WhiteSpaces
Represents whitespace characters recognized by TypeScript.
type Spaces = WhiteSpaces
Falsy
Represents falsy values: null
, undefined
, 0
, false
, or ""
.
type FalsyValue = Falsy