TypeScript Miscellaneous Utilities

0.1.1 · active · verified Sun Apr 19

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

Warnings

Install

Imports

Quickstart

This quickstart demonstrates the usage of `Dict` for creating flexible object types, `ElementOf` for inferring types from tuples/arrays, and `ParametersOf`/`ReturnOf` for extracting function signature details.

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);

view raw JSON →