TypeScript Helper Types Collection

0.0.19 · active · verified Sun Apr 19

helpertypes is a lightweight collection of general-purpose TypeScript helper types designed to simplify complex type manipulations across various projects. As of version `0.0.19`, the library offers utilities like deep partial/required types, object key pickers, and string lookup types. The project maintains an active development status, with frequent releases (often multiple per month) focused on introducing new helper types and refining the behavior and robustness of existing ones, particularly for deep type transformations. Its key differentiators include its small footprint, focused scope on just type utilities without runtime code, and a commitment to providing common, reusable type patterns that might otherwise be cumbersome to implement from scratch. While still in a pre-1.0 state, it's regularly updated with fixes and new features.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to install `helpertypes` and use `PartialDeep`, `RequiredDeep`, and `ObjectLookupString` to create new derived types from existing interfaces, showcasing deep optionality, deep requiredness, and string-based path lookups.

import type { PartialDeep, RequiredDeep, ObjectLookupString } from 'helpertypes';

interface UserProfile {
  id: string;
  name: string;
  settings: {
    theme: 'dark' | 'light';
    notifications: boolean;
  };
  tags?: string[];
}

// Create a deeply partial version of UserProfile
type PartialUserProfile = PartialDeep<UserProfile>;

const partialUser: PartialUserProfile = {
  id: 'abc-123',
  settings: { theme: 'dark' } // Only define some nested properties
};

console.log(partialUser);

// Create a deeply required version, making all optional properties required (if possible)
type FullyRequiredUserProfile = RequiredDeep<UserProfile>;

const fullUser: FullyRequiredUserProfile = {
  id: 'def-456',
  name: 'Jane Doe',
  settings: { theme: 'light', notifications: true },
  tags: [] // 'tags' is now required
};

console.log(fullUser);

interface NestedObject {
  a: { b: { c: string } };
  d: number;
}

// Look up a string path in a nested object type
type PathA_B_C = ObjectLookupString<NestedObject, 'a.b.c'>; // Expected: string
type PathD = ObjectLookupString<NestedObject, 'd'>;         // Expected: number

// Type checking for example usage (won't run in JS)
const val1: PathA_B_C = 'hello';
// @ts-expect-error - Type 'number' is not assignable to type 'string'.
const val2: PathA_B_C = 123;

console.log('Demonstrating type utilities. Check TypeScript compiler for full effect.');

view raw JSON →