Essential TypeScript Utility Types

1.0.0 · active · verified Sun Apr 19

util-ts-types is a TypeScript utility library providing a curated collection of essential, reusable type constructs designed to simplify complex type manipulations within TypeScript projects. Currently at version 1.0.0, this package appears to be primarily maintained for internal consumption, evidenced by its 'private' designation in the README. As a type-only library, its release cadence is likely tied to internal project needs rather than public API changes, implying stability but infrequent external updates for the broader community. Key differentiators typically involve a set of opinionated or project-specific utility types that might not be available directly in TypeScript's built-in `lib.d.ts` or widely adopted libraries like `type-fest`, focusing on solving specific organizational typing challenges without introducing runtime dependencies.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates the application of hypothetical `DeepPartial`, `Mutable`, and `Maybe` utility types to transform existing TypeScript interfaces and types, showcasing their primary use cases in data manipulation and type refinement.

import type { DeepPartial, Mutable, Maybe } from 'util-ts-types';

// Imagine util-ts-types provides these utilities:

interface UserProfile {
  readonly id: string;
  name: string;
  email: string;
  settings?: { theme: string; notifications: boolean };
}

// DeepPartial: Makes all properties and sub-properties optional and mutable.
type PartialUserProfile = DeepPartial<UserProfile>;

const updates: PartialUserProfile = {
  name: 'Jane Doe',
  settings: { theme: 'dark' } // 'notifications' is optional
};

// Mutable: Removes 'readonly' modifiers from properties.
type WritableUserProfile = Mutable<UserProfile>;

let user: WritableUserProfile = {
  id: 'abc-123', // 'id' is now writable
  name: 'John Doe',
  email: 'john@example.com'
};
user.id = 'new-id-456'; // This would error without Mutable

// Maybe: Wraps a type to include 'null' or 'undefined'.
type OptionalEmail = Maybe<string>;

const primaryEmail: OptionalEmail = 'user@example.com';
const secondaryEmail: OptionalEmail = null;
const tertiaryEmail: OptionalEmail = undefined;

console.log(`User email: ${primaryEmail ?? 'Not provided'}`);

// Function demonstrating usage
function updateProfile(current: UserProfile, patch: PartialUserProfile): UserProfile {
  return { ...current, ...patch, settings: { ...current.settings, ...patch.settings } };
}

const initialProfile: UserProfile = { id: '1', name: 'Original', email: 'orig@example.com' };
const updated = updateProfile(initialProfile, { name: 'New Name' });
console.log(`Updated profile name: ${updated.name}`);

view raw JSON →