TypeScript Essentials

10.1.1 · active · verified Sun Apr 19

ts-essentials is a comprehensive collection of advanced TypeScript utility types designed to enhance type safety and improve developer experience beyond the standard library. As of version 10.1.1, it provides a wide array of types like `Prettify`, `DeepReadonly`, `StrictOmit`, and robust path-based utilities for complex object transformations, such as `Paths` and `PathValue`. The library is actively maintained with frequent patch and minor releases, typically occurring every few weeks, addressing bug fixes and introducing new utility types. Key differentiators include stricter versions of built-in utility types (e.g., `StrictExclude`), deep transformation types like `DeepRequired` and `DeepMarkOptional`, and sophisticated path-based type manipulation. It requires `typescript>=4.5.0` as a peer dependency and mandates the `strictNullChecks` compiler option in `tsconfig.json` for optimal functionality and type correctness, ensuring a higher level of type strictness.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates the usage of `Prettify` for readability, `DeepReadonly` for immutability, `StrictOmit` for precise property exclusion, and `Paths`/`PathValue` for advanced path-based type introspection on a complex object.

import { Prettify, DeepReadonly, StrictOmit, Paths, PathValue } from 'ts-essentials';

interface User {
  id: string;
  name: string;
  email?: string;
  address: {
    street: string;
    city: string;
    zip: number;
    coordinates?: [number, number];
  };
  preferences: {
    theme: 'dark' | 'light';
    notifications: boolean;
  };
  roles: ('admin' | 'editor' | 'viewer')[];
}

// Prettify: Flattens and makes types more readable in IDE tooltips
type PrettyUser = Prettify<User>;

// DeepReadonly: Makes all properties and nested properties readonly
type ImmutableUser = DeepReadonly<User>;
// ImmutableUser.address.street = 'New Street'; // Type Error: Cannot assign to 'street' because it is a read-only property.

// StrictOmit: Constructs a type by picking all properties from Type and then removing Keys.
// It is a stricter version of TypeScript's built-in Omit.
type UserWithoutSensitiveInfo = StrictOmit<User, 'email' | 'address.coordinates' | 'roles'>;

const newUser: UserWithoutSensitiveInfo = {
  id: 'abc-123',
  name: 'Jane Doe',
  address: { street: 'Main St', city: 'Anytown', zip: 12345 },
  preferences: { theme: 'dark', notifications: true }
  // email: 'jane@example.com' // Type Error: Object literal may only specify known properties
};

// Paths: Generates a union of all possible string paths within an object type
type UserPathExamples = Paths<User>;
// UserPathExamples could include 'id' | 'address.city' | 'preferences.theme' | 'roles.0'

// PathValue: Retrieves the type of the value at a specific path
type AddressCityType = PathValue<User, 'address.city'>; // string
type FirstRoleType = PathValue<User, 'roles.0'>; // 'admin' | 'editor' | 'viewer'

view raw JSON →