TypeScript Utility Types Collection

3.11.0 · active · verified Sun Apr 19

utility-types is a comprehensive collection of TypeScript utility types designed to complement and extend TypeScript's built-in mapped types and aliases. Currently stable at v3.11.0, the library maintains an active release schedule, frequently introducing new type utilities and refining existing ones to keep pace with TypeScript's evolution and community needs. It differentiates itself by offering a broad range of idiomatic types, including those compatible with Flow's utility types to aid migration efforts, all without introducing any runtime cost. The package prides itself on being secure and minimal, having no third-party dependencies and ensuring type correctness through rigorous testing with `dts-jest`. Its primary goal is to provide developers with a robust, zero-cost 'lodash' for static types, eliminating the need to re-implement common type patterns across projects.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use several key utility types from the `utility-types` library, including `DeepReadonly` for deep immutability, `Optional` for making specific properties optional, and `Falsy`, `Primitive`, and `Nullish` for working with common JavaScript value classifications at the type level.

import { DeepReadonly, Optional, Falsy, Primitive, Nullish } from 'utility-types';

interface User {
  id: number;
  name: string;
  email?: string;
  settings: {
    theme: 'dark' | 'light';
    notificationsEnabled: boolean;
  };
}

// DeepReadonly: Makes all properties, including nested ones, readonly.
type ImmutableUser = DeepReadonly<User>;

const user: ImmutableUser = {
  id: 1,
  name: 'Alice',
  settings: {
    theme: 'dark',
    notificationsEnabled: true,
  },
};
// user.name = 'Bob'; // Error: Cannot assign to 'name' because it is a read-only property.
// user.settings.theme = 'light'; // Error: Cannot assign to 'theme' because it is a read-only property.

// Optional<T, K>: Makes specific keys K in T optional.
type UserWithOptionalId = Optional<User, 'id'>;
const newUser: UserWithOptionalId = {
  name: 'Bob',
  settings: { theme: 'light', notificationsEnabled: false },
};

// Falsy: Represents common "falsy" values in JavaScript.
function isFalsyValue(value: unknown): value is Falsy {
  return !value; // A simple runtime check for demonstration
}
console.log(`Is 0 falsy? ${isFalsyValue(0)}`); // true
console.log(`Is 'hello' falsy? ${isFalsyValue('hello')}`); // false

// Primitive: Represents non-object types.
type MyPrimitive = Primitive;
let p: MyPrimitive = 123;
p = 'hello';
p = true;
// p = { a: 1 }; // Error: Type '{ a: number; }' is not assignable to type 'Primitive'.

// Nullish: Represents null or undefined.
type MaybeString = string | Nullish;
const s1: MaybeString = "hello";
const s2: MaybeString = null;
const s3: MaybeString = undefined;
// const s4: MaybeString = false; // Error: Type 'false' is not assignable to type 'string | null | undefined'.

// This quickstart demonstrates how to use several key utility types from the `utility-types`
// library, including `DeepReadonly` for deep immutability, `Optional` for making specific
// properties optional, and `Falsy`, `Primitive`, and `Nullish` for working with common
// JavaScript value classifications at the type level.

view raw JSON →