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 { Prettify, LiteralUnion, ReturnTypeOf } from "@halvaradop/ts-utility-types/utils"
import type * as Utils from "@halvaradop/ts-utility-types/utils"
type Utilities = Utils.- 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.
Argument | Type | Description |
---|---|---|
Obj | object | The object type to prettify. |
import type { Prettify } from "@halvaradop/ts-utility-types/utils"
type Pretty = Prettify<{ foo: string; bar: number }>
LiteralUnion<T, U = string>
Extends the values of a literal type with a broader type (defaults to string
).
Argument | Type | Description |
---|---|---|
T | U | The literal type(s) to extend. |
U | string | The base type to extend with (default string). |
import type { LiteralUnion } from "@halvaradop/ts-utility-types/utils"
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.
Argument | Type | Description |
---|---|---|
Type | any | The union type to filter. |
Extends | any | The type to check assignability against. |
Value | any | The value to use for excluded/included types (default never). |
Reverse | boolean | If true, includes matching types; if false, excludes them (default false). |
import type { Discard } from "@halvaradop/ts-utility-types/utils"
type A = Discard<string | number, string>
type B = Discard<string | number, boolean>
ArgsFunction
Represents a function that accepts any number of arguments of any type.
import type { ArgsFunction } from "@halvaradop/ts-utility-types/utils"
const fn: ArgsFunction = (...args) => {}
Nullish
Represents the absence of value: null
or undefined
.
import type { Nullish } from "@halvaradop/ts-utility-types/utils"
type CanBeNull = string | Nullish
PrimitiveNullish
A primitive type that can also be null
or undefined
.
import type { PrimitiveNullish } from "@halvaradop/ts-utility-types/utils"
type PrimitiveOrNull = PrimitiveNullish
Primitive
A primitive type: number
, string
, boolean
, bigint
, or symbol
(excludes nullish).
import type { Primitive } from "@halvaradop/ts-utility-types/utils"
type PrimitiveValues = Primitive
WhiteSpaces
Represents whitespace characters recognized by TypeScript.
import type { WhiteSpaces } from "@halvaradop/ts-utility-types/utils"
type Spaces = WhiteSpaces
Falsy
Represents falsy values: null
, undefined
, 0
, false
, or ""
.
import type { Falsy } from "@halvaradop/ts-utility-types/utils"
type FalsyValue = Falsy
ReturnTypeOf<T>
Determines the primitive type corresponding to the provided value.
Argument | Type | Description |
---|---|---|
T | any | The value to get the primitive type of. |
import type { ReturnTypeOf } from "@halvaradop/ts-utility-types/utils"
type TypeOfValue = ReturnTypeOf<123>
type TypeOfValueString = ReturnTypeOf<"hello">
type TypeOfValueBool = ReturnTypeOf<true>
type TypeOfValueArr = ReturnTypeOf<[1, 2, 3]>
Type-Level Testing
Utilities for type-level testing and assertions in TypeScript. These types help compare, validate, and ensure type conditions in testing or advanced development scenarios.
Deprecated - Types Entry Point
The /types entry point is deprecated and will be removed in the next major version. Please use the default export from @halvaradop/ts-utility-types or import specific types from the appropriate entry point.