Deep Utility Types
Deep Utility Types is a TypeScript-focused library providing advanced utility types for performing operations like `Omit`, `Pick`, `Require`, and `Optional` on nested object structures. Unlike standard TypeScript utility types, this library supports dot-notation for accessing and modifying properties at any depth within an object, including handling arrays. It is fully type-safe, offers excellent autocompletion support in IDEs, and includes a mechanism for ignoring specific types (like large classes or built-ins) to improve type-checker performance and reduce noise in autocompletion suggestions. The current stable version is 1.3.1, with releases typically focusing on bug fixes and incremental features, such as exposing internal types for broader use. Its primary differentiator is its robust deep-nesting support and type-safety for complex object manipulations, which is not natively provided by TypeScript.
Common errors
-
Cannot find module 'deep-utility-types' or its corresponding type declarations.
cause The package is not installed, or TypeScript cannot locate its declarations, possibly due to incorrect `tsconfig.json` setup or trying to import in a non-TypeScript file.fixInstall the package: `npm install --save-dev deep-utility-types` or `yarn add --dev deep-utility-types`. Ensure your files are being processed by TypeScript and `tsconfig.json` includes the relevant directories. -
Type 'Partial<OmittedType>' is not assignable to type 'OmittedType'. Property 'foo2' is missing in type 'Partial<OmittedType>' but required in type 'OmittedType'.
cause Incorrectly applying the deep utility type, or trying to assign an incomplete object where properties were expected to be optional but remain required by the new type definition.fixCarefully review the keys provided to `DeepOmit` or `DeepPick` to ensure they accurately reflect the desired output type. If properties should be optional, consider using `DeepOptional` or explicitly marking the resultant type as `Partial<T>` if the intention is to allow partial assignment.
Warnings
- gotcha This library provides only TypeScript type definitions; it has no runtime JavaScript code. Attempting to use `require()` or expecting runtime behavior will result in errors.
- breaking When working with `KeysAsDotNotation` and union types, versions prior to v1.3.1 might incorrectly handle the union, leading to unexpected or incomplete key paths.
- gotcha The `KeysAsDotNotation` and `DefaultIgnoredTypes` were explicitly exposed as exportable types starting from version 1.3.0. If you are on an older version, these types will not be directly importable.
- gotcha For types with very large or complex properties (like many built-in JavaScript classes such as `Function`, `Map`, `Promise`), not utilizing the `IgnoredTypes` generic parameter can lead to degraded type-checker performance and cluttered autocompletion suggestions.
Install
-
npm install deep-utility-types -
yarn add deep-utility-types -
pnpm add deep-utility-types
Imports
- DeepOmit
const { DeepOmit } = require('deep-utility-types');import { DeepOmit } from 'deep-utility-types'; - DeepPick
import { DeepPick } from 'deep-utility-types'; - KeysAsDotNotation
import { KeysAsDotNotation } from 'deep-utility-types';
Quickstart
import { DeepOmit } from 'deep-utility-types';
type UserProfile = {
id: string;
name: string;
contact: {
email: string;
phone: string;
address: {
street: string;
city: string;
zip: string;
};
};
settings: {
notifications: boolean;
theme: 'light' | 'dark';
};
};
// Create a type that omits the user's phone number and notification setting
type PublicUserProfile = DeepOmit<UserProfile, 'contact.phone' | 'settings.notifications'>;
const user: PublicUserProfile = {
id: 'user-123',
name: 'John Doe',
contact: {
email: 'john.doe@example.com',
address: {
street: '123 Main St',
city: 'Anytown',
zip: '12345',
},
},
settings: {
theme: 'dark',
},
};
console.log(user);