Skip to main content
The Type Helpers package provides a comprehensive set of TypeScript utilities to enhance type safety and developer experience in your applications.

Installation

npm install @zayne-labs/toolkit-type-helpers

What’s Included

Type Utilities

Advanced TypeScript type transformations including Prettify, Writeable, and union manipulation utilities

Guards & Assertions

Runtime type guards and assertion functions for safe type narrowing

Enum Utilities

Type-safe enum creation with powerful type inference capabilities

Quick Start

Type Guards

Use type guards to safely narrow types at runtime:
import { isString, isNumber, isArray } from '@zayne-labs/toolkit-type-helpers';

const value: unknown = "hello";

if (isString(value)) {
  // TypeScript knows value is string here
  console.log(value.toUpperCase());
}

Type Utilities

Transform complex types with ease:
import type { Prettify, Writeable } from '@zayne-labs/toolkit-type-helpers';

type ReadonlyUser = {
  readonly id: string;
  readonly name: string;
};

// Remove readonly modifiers
type MutableUser = Writeable<ReadonlyUser>;
// Result: { id: string; name: string; }

Assertions

Enforce type constraints with assertions:
import { assert, assertDefined } from '@zayne-labs/toolkit-type-helpers';

function processUser(user: User | null) {
  assertDefined(user);
  // TypeScript knows user is non-null here
  console.log(user.name);
}

Enums

Create type-safe enums with union extraction:
import { defineEnum } from '@zayne-labs/toolkit-type-helpers';

const Status = defineEnum({
  PENDING: 'pending',
  ACTIVE: 'active',
  COMPLETED: 'completed'
} as const, { inferredUnionVariant: 'values' });

type StatusValue = typeof Status.$inferUnion;
// Result: 'pending' | 'active' | 'completed'

Features

  • Prettify: Flatten intersection types for better IntelliSense
  • Writeable: Remove readonly modifiers (shallow or deep)
  • UnmaskType: Display computed types instead of aliases
  • DistributiveOmit/Pick: Omit or pick properties from union types
  • Primitive guards: isString, isNumber, isBoolean, isSymbol
  • Object guards: isObject, isPlainObject, isArray
  • Special guards: isPromise, isFunction, isAsyncFunction
  • Serialization guards: isJSONSerializable, isValidJsonString
  • assert: General assertion with custom messages
  • assertDefined: Assert non-null/undefined values
  • assertENV: Validate environment variables
  • AssertionError: Custom error class for failures
  • ExtractUnion: Extract union types from objects or arrays
  • UnionDiscriminator: Create discriminated unions
  • UnionToIntersection: Convert unions to intersections
  • LiteralUnion: Combine literal types with autocomplete

Package Information

Next Steps

Type Utilities

Explore type transformation utilities

Guards & Assertions

Learn about runtime type safety

Enums

Create type-safe enums

Build docs developers (and LLMs) love