TypeScript Miscellaneous Utilities
The `typescript-miscellaneous` package offers a collection of advanced TypeScript utility types designed to simplify complex type manipulations and enhance type safety within TypeScript projects. Currently at version 0.1.1, it provides specialized types such as `Dict` for creating flexible dictionary-like structures, `UnionToIntersection` for converting union types into intersection types, `ElementOf` for inferring element types from array or tuple types, and `ParametersOf` and `ReturnOf` for extracting function signature components. As a focused utility library, its release cadence is typically infrequent, prioritizing stability and correctness over rapid feature expansion. It distinguishes itself by providing pre-built, reusable type constructs that address common, yet often complex, TypeScript type challenges, thereby reducing boilerplate and improving developer productivity.
Common errors
-
Cannot find name 'Dict'.
cause The utility type was not imported into the current file.fixAdd the necessary import statement: `import { Dict } from 'typescript-miscellaneous';` (or the specific type you are using). -
Type 'X' is not assignable to type 'Y'.
cause An incorrect input type was provided to one of the utility types, or the resulting type was misused.fixCarefully review the documentation for the specific utility type you are using and ensure your input types and usage patterns align with its expected behavior. -
An import path cannot end with a '.js' extension when 'resolveJsonModule' is enabled. Consider importing 'foo' instead.
cause This package primarily exports TypeScript types. While the error might be generic to module resolution, it could imply misconfiguration if you're trying to import types from a `.js` output or incorrectly configuring module resolution for type files.fixEnsure your `tsconfig.json` `moduleResolution` is set to `node` or `bundler` and `allowSyntheticDefaultImports` and `esModuleInterop` are enabled if using default imports (though this library primarily uses named type imports). Avoid adding `.js` extensions to type import paths.
Warnings
- breaking This library explicitly requires TypeScript version 3.0.0 or higher. Using it with older TypeScript compilers will result in compilation errors due to reliance on newer type system features.
- gotcha As a library in its 0.x.x version, the API surface might undergo breaking changes in minor or patch releases before a stable 1.0.0 version is published. Always review the changelog when upgrading.
- gotcha Advanced utility types, especially when deeply nested or heavily composed, can sometimes lead to increased TypeScript compilation times and memory usage. Monitor build performance in large codebases.
- gotcha Some complex type manipulations might result in unexpected type inference behavior when combined with other highly generic types or specific TypeScript strictness settings. Debug type issues using TypeScript's `--traceResolution` or IDE type inspection tools.
Install
-
npm install typescript-miscellaneous -
yarn add typescript-miscellaneous -
pnpm add typescript-miscellaneous
Imports
- Dict
const Dict = require('typescript-miscellaneous').Dict;import { Dict } from 'typescript-miscellaneous'; - UnionToIntersection
const UnionToIntersection = require('typescript-miscellaneous').UnionToIntersection;import { UnionToIntersection } from 'typescript-miscellaneous'; - ElementOf
import * as Lib from 'typescript-miscellaneous'; type El = Lib.ElementOf<any>;
import { ElementOf } from 'typescript-miscellaneous';
Quickstart
import { Dict, ElementOf, ParametersOf, ReturnOf } from 'typescript-miscellaneous';
// Example 1: Dict - create a dictionary-like type with mixed keys and values
type MyComplexDict = Dict<
'id' | 'name' | 'age' | 0 | 1,
string | number
>;
const user: MyComplexDict = {
id: 'uuid-123',
name: 'Alice',
age: 30,
0: 'first_val',
1: 123
};
console.log(`User ID: ${user.id}, Age: ${user.age}`);
// Example 2: ElementOf - extract the union type of elements from an array/tuple
type Colors = ['red', 'green', 'blue'];
type Color = ElementOf<Colors>; // Expected: 'red' | 'green' | 'blue'
const favoriteColor: Color = 'green';
// const invalidColor: Color = 'yellow'; // This would correctly cause a TypeScript error
console.log(`Favorite color: ${favoriteColor}`);
// Example 3: ParametersOf and ReturnOf - infer parts of a function signature
function greet(name: string, age: number): string {
return `Hello, ${name}! You are ${age} years old.`;
}
type GreetParams = ParametersOf<typeof greet>; // Expected: [name: string, age: number]
type GreetReturn = ReturnOf<typeof greet>; // Expected: string
const params: GreetParams = ['Bob', 25];
const greeting: GreetReturn = greet(...params);
console.log(greeting);