Essential TypeScript Utility Types
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
-
Cannot find module 'util-ts-types' or its corresponding type declarations.
cause The package is not installed, or your TypeScript configuration (e.g., `compilerOptions.typeRoots`, `paths`) is incorrect, or you are trying to `require()` it in a runtime context.fixEnsure 'util-ts-types' is listed in your `package.json` and installed. Verify `tsconfig.json` paths. For type-only imports, ensure `import type` syntax is used. -
'util-ts-types' has no exported member 'SomeType'. Did you mean to use a default import?
cause You are trying to import a type that is not actually exported by the library, or the case of the type name is incorrect.fixCheck the `index.d.ts` file within the `util-ts-types` package to confirm the exact names and exports available. Adjust your import statement accordingly. -
Type 'X' is not assignable to type 'Y'.
cause A utility type from `util-ts-types` was applied, but the resulting type `Y` is not compatible with the expected type `X` in your assignment or function call.fixReview the definition of the utility type and how it transforms `X` into `Y`. Adjust your input type or expected type to match the transformation, or rethink the use of that specific utility type.
Warnings
- gotcha This package is explicitly marked as '(Private)' in its README. While functional, it might not adhere to standard public API stability guarantees. Consumers should be aware that breaking changes might occur without semantic versioning considerations typical for public npm packages.
- gotcha As a type-only library, `util-ts-types` contains no runtime JavaScript code. Attempting to import it using `require()` in CommonJS environments or directly referencing it as a runtime dependency will lead to module resolution errors or unexpected behavior at runtime.
- gotcha The specific utility types provided by this package are not detailed in the truncated README. There's a risk that inferred or commonly expected utility types (e.g., `DeepPartial`, `Mutable`) may not actually be exported, leading to 'has no exported member' errors.
- gotcha Reliance on specific TypeScript versions: Utility type libraries often leverage advanced TypeScript features. While `1.0.0` suggests stability, older TypeScript versions might not fully support all internal type definitions or lead to unexpected type inference issues.
Install
-
npm install util-ts-types -
yarn add util-ts-types -
pnpm add util-ts-types
Imports
- DeepPartial
import { DeepPartial } from 'util-ts-types';import type { DeepPartial } from 'util-ts-types'; - Mutable
const { Mutable } = require('util-ts-types');import type { Mutable } from 'util-ts-types'; - Maybe
import * as UtilTypes from 'util-ts-types';
import type { Maybe } from 'util-ts-types';
Quickstart
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}`);