or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

aliases-guards.mdflow-utilities.mdindex.mdmapped-types.md
tile.json

tessl/npm-utility-types

Utility Types Collection for TypeScript providing comprehensive type-level utilities for static type manipulation.

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/utility-types@3.11.x

To install, run

npx @tessl/cli install tessl/npm-utility-types@3.11.0

index.mddocs/

Utility Types

Utility Types is a comprehensive collection of TypeScript utility types that complement built-in mapped types and aliases. It provides the essential type-level utilities for TypeScript developers, serving as the "lodash" for static types with zero runtime cost and no dependencies.

Package Information

  • Package Name: utility-types
  • Package Type: npm
  • Language: TypeScript
  • Installation: npm install utility-types
  • Zero Runtime Cost: Yes
  • Dependencies: None

Core Imports

import { 
  // Flow-style utilities
  $Keys, $Values, $ReadOnly, $Diff, $PropertyType, $ElementType, 
  $Call, $Shape, $NonMaybeType, Class,
  
  // Advanced mapped types
  SetIntersection, SetDifference, Diff, Omit, Optional, Required,
  DeepReadonly, DeepRequired, DeepPartial, DeepNonNullable,
  PickByValue, FunctionKeys, NonFunctionKeys, Mutable, Brand,
  
  // Type aliases and guards
  Primitive, Falsy, Nullish, isPrimitive, isFalsy, isNullish,
  
  // Deprecated (do not use in new code)
  getReturnOfExpression
} from 'utility-types';

For specific imports:

import { Omit, DeepReadonly, $Keys } from 'utility-types';

Basic Usage

import { Omit, DeepReadonly, $Keys, Optional, PickByValue } from 'utility-types';

// Basic object manipulation
interface User {
  id: number;
  name: string;
  email: string;
  password: string;
}

// Remove sensitive fields
type PublicUser = Omit<User, 'password'>;
// Result: { id: number; name: string; email: string; }

// Make some fields optional
type UserUpdate = Optional<User, 'id' | 'email'>;
// Result: { id?: number; name: string; email?: string; password: string; }

// Extract keys as union type
type UserKeys = $Keys<User>;
// Result: "id" | "name" | "email" | "password"

// Deep readonly for nested objects
interface Settings {
  theme: { mode: 'light' | 'dark'; colors: string[] };
  preferences: { notifications: boolean; autoSave: boolean };
}

type ReadonlySettings = DeepReadonly<Settings>;
// Result: All properties and nested properties become readonly

// Pick properties by value type
interface Mixed {
  id: number;
  name: string;
  count: number;
  active: boolean;
}

type NumberFields = PickByValue<Mixed, number>;
// Result: { id: number; count: number; }

Architecture

Utility Types is organized into four logical modules:

  • Flow-style Utilities: Flow.js compatibility types for migration and interoperability
  • Advanced Mapped Types: Complex type transformations, key selection, and object manipulation
  • Type Aliases and Guards: Common type definitions with runtime type checking functions
  • Deep Transformations: Recursive type operations for nested data structures

The library uses conditional types, mapped types, and type inference to provide compile-time type transformations without any runtime overhead.

Capabilities

Flow-style Utilities

Flow.js compatible utility types that enable easy migration from Flow to TypeScript and provide familiar APIs for Flow developers.

type $Keys<T extends object> = keyof T;
type $Values<T extends object> = T[keyof T];
type $ReadOnly<T extends object> = DeepReadonly<T>;
type $Diff<T extends U, U extends object> = Pick<T, SetComplement<keyof T, keyof U>>;
type $PropertyType<T extends object, K extends keyof T> = T[K];
type $ElementType<T, K extends keyof T | number> = T[K];
type $Call<Fn extends (...args: any[]) => any> = Fn extends (arg: any) => infer RT ? RT : never;
type $Shape<T extends object> = Partial<T>;
type $NonMaybeType<T> = NonNullable<T>;
type Class<T> = new (...args: any[]) => T;

Flow-style Utilities

Advanced Mapped Types

Sophisticated type transformations for object manipulation, key selection, and complex type operations including set operations and property filtering.

// Set operations
type SetIntersection<A, B> = A extends B ? A : never;
type SetDifference<A, B> = A extends B ? never : A;
type SetComplement<A, A1 extends A> = SetDifference<A, A1>;

// Object manipulation
type Omit<T, K extends keyof any> = Pick<T, SetDifference<keyof T, K>>;
type Optional<T extends object, K extends keyof T = keyof T> = Omit<T, K> & Partial<Pick<T, K>>;
type Required<T extends object, K extends keyof T = keyof T> = Omit<T, K> & Required<Pick<T, K>>;

// Key selection
type FunctionKeys<T extends object> = {
  [K in keyof T]-?: NonUndefined<T[K]> extends Function ? K : never;
}[keyof T];
type OptionalKeys<T> = {
  [K in keyof T]: {} extends Pick<T, K> ? K : never;
}[keyof T];

// Value-based selection
type PickByValue<T, ValueType> = Pick<T, {
  [Key in keyof T]: T[Key] extends ValueType ? Key : never;
}[keyof T]>;

Advanced Mapped Types

Type Aliases and Guards

Common type definitions with corresponding runtime type guard functions for type checking at runtime.

type Primitive = string | number | bigint | boolean | symbol | null | undefined;
type Falsy = false | '' | 0 | null | undefined;
type Nullish = null | undefined;

function isPrimitive(val: unknown): val is Primitive;
function isFalsy(val: unknown): val is Falsy;  
function isNullish(val: unknown): val is Nullish;

Type Aliases and Guards

Deep Transformations

Recursive type operations that work on deeply nested data structures, providing consistent behavior across all nesting levels.

type DeepReadonly<T> = T extends ((...args: any[]) => any) | Primitive
  ? T
  : T extends _DeepReadonlyArray<infer U>
  ? _DeepReadonlyArray<U>
  : T extends _DeepReadonlyObject<infer V>
  ? _DeepReadonlyObject<V>
  : T;

type DeepRequired<T> = T extends (...args: any[]) => any
  ? T
  : T extends any[]
  ? _DeepRequiredArray<T[number]>
  : T extends object
  ? _DeepRequiredObject<T>
  : T;

type DeepPartial<T> = { [P in keyof T]?: _DeepPartial<T[P]> };

type DeepNonNullable<T> = T extends (...args: any[]) => any
  ? T
  : T extends any[]
  ? _DeepNonNullableArray<T[number]>
  : T extends object
  ? _DeepNonNullableObject<T>
  : T;

These transformations handle functions, arrays, and objects appropriately, ensuring type safety throughout complex nested structures.

Type Definitions

Common Supporting Types

type NonUndefined<A> = A extends undefined ? never : A;

type UnionToIntersection<U> = (U extends any ? (k: U) => void : never) extends (k: infer I) => void ? I : never;

type Brand<T, U> = T & { __brand: U };

type PromiseType<T extends Promise<any>> = T extends Promise<infer U> ? U : never;

type ValuesType<T extends ReadonlyArray<any> | ArrayLike<any> | Record<any, any>> = 
  T extends ReadonlyArray<any>
    ? T[number]
    : T extends ArrayLike<any>
    ? T[number]
    : T extends object
    ? T[keyof T]
    : never;

// Deep transformation helper types
interface _DeepReadonlyArray<T> extends ReadonlyArray<DeepReadonly<T>> {}
type _DeepReadonlyObject<T> = { readonly [P in keyof T]: DeepReadonly<T[P]> };

type _DeepRequiredArray<T> = Array<DeepRequired<NonUndefined<T>>>;
type _DeepRequiredObject<T> = { [P in keyof T]-?: DeepRequired<NonUndefined<T[P]>> };

type _DeepNonNullableArray<T> = Array<DeepNonNullable<NonNullable<T>>>;
type _DeepNonNullableObject<T> = { [P in keyof T]-?: DeepNonNullable<NonNullable<T[P]>> };

type _DeepPartial<T> = T extends Function
  ? T
  : T extends ReadonlyArray<infer U>
  ? _DeepPartialArray<U>
  : T extends object
  ? DeepPartial<T>
  : T;

type _DeepPartialArray<T> = Array<_DeepPartial<T>>;