TypeScript Utility Types

A comprehensive collection of utility types to enhance productivity and improve code readability in TypeScript projects.

TypeScript Utility Types

A powerful collection of utility types designed to enhance your TypeScript development experience. This package provides type-level operations for arrays, objects, strings, numbers, validation, and advanced deep manipulations, all built from scratch without relying on TypeScript's built-in utility types.

All utility types are zero-runtime and provide excellent IntelliSense support with comprehensive type checking.

Features

  • ๐Ÿ”ง Comprehensive Type Operations: Over 100+ utility types for common and advanced use cases
  • ๐ŸŽฏ Type-Safe: All utilities are fully type-safe with excellent IntelliSense support
  • ๐Ÿ“ฆ Modular: Import only what you need with tree-shakable exports
  • ๐Ÿš€ Zero Runtime: Pure TypeScript types with no runtime overhead
  • ๐Ÿงช Well-Tested: Extensively tested with type-level assertions
  • ๐Ÿ“š Excellent DX: Comprehensive documentation with practical examples

Installation

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

Quick Start

Install the package as a development dependency in your TypeScript project.

Import the types you need from the appropriate module:

import type { DeepMerge } from "@halvaradop/ts-utility-types/deep"
import type { ArrayToUnion } from "@halvaradop/ts-utility-types/arrays"
import type { Capitalize } from "@halvaradop/ts-utility-types/strings"
import { isObject, isArray } from "@halvaradop/ts-utility-types/validate"

Use the types in your TypeScript code:

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

interface Config {
  : {
    : number
  }
  : {
    : boolean
  }
}

interface UserConfig {
  : {
    : number
  }
  : {
    : boolean
  }
}

type AppConfig = <Config, UserConfig>
type AppConfig = {
    api: {
        timeout: number;
        retries: number;
    };
    features: {
        darkMode: boolean;
        analytics: boolean;
    };
}

Available Modules

Common Usage Patterns

Conditional Types

import type { IsArray, IsObject } from "@halvaradop/ts-utility-types/type-guards"

type ProcessData<T> =
  IsArray<T> extends true
    ? T[number] // Extract array element type
    : IsObject<T> extends true
      ? keyof T // Extract object keys
      : T // Return as-is for primitives

Advanced Object Manipulation

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

interface ApiResponse {
  : {
    : { : string; : string }
    : { : string; : boolean }
  }
}

type PartialApiResponse = <ApiResponse>
type PartialApiResponse = {
    user?: {
        profile?: {
            name?: string | undefined;
            email?: string | undefined;
        } | undefined;
        settings?: {
            theme?: string | undefined;
            notifications?: boolean | undefined;
        } | undefined;
    } | undefined;
}
type RequiredApiResponse = <>
type RequiredApiResponse = {
    user: {
        profile: {
            name: string;
            email: string;
        };
        settings: {
            theme: string;
            notifications: boolean;
        };
    };
}
type ImmutableApiResponse = <ApiResponse>
type ImmutableApiResponse = {
    readonly user: {
        readonly profile: {
            readonly name: string;
            readonly email: string;
        };
        readonly settings: {
            readonly theme: string;
            readonly notifications: boolean;
        };
    };
}

Performance Note

All utility types are designed to be efficient at compile-time with recursive depth limits and tail recursion optimization where possible.

TypeScript Compatibility

  • TypeScript 4.5+: Core functionality
  • TypeScript 4.8+: Improved template literal types
  • TypeScript 5.0+: Enhanced recursive type handling

Need Help?

Visit our Community & Support page to:

  • ๐Ÿ› Report bugs or request features
  • ๐Ÿ’ฌ Join community discussions
  • ๐Ÿค Learn how to contribute
  • ๐Ÿ“š Find additional resources

Ready to get started? Choose a module from the navigation or explore the Arrays section for practical examples.