TypeScript Type Utils

0.1.0 · active · verified Sun Apr 19

typescript-type-utils is a lightweight, zero-runtime collection of TypeScript utility types designed to enhance type safety and enable advanced type manipulations within projects. At version 0.1.0, it provides a set of 'test-kit' types for compile-time API signature verification (e.g., `ExpectTrue`, `Equal`) and 'tuple-utils' for advanced tuple type transformations (e.g., `TupleToUnion`, `ReduceTupleOn`). A key differentiator is its strict design principle: the package explicitly contains only `.d.ts` declaration files, meaning it ships no actual JavaScript code, thereby ensuring zero runtime footprint and no potential for runtime errors. Its release cadence is likely infrequent given its stable, pure-type nature and early version number, focusing on robust and broadly applicable type definitions rather than rapid feature additions.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates how to import and use utility types like `Equal`, `ExpectTrue`, and `TupleToUnion` for compile-time type assertions and transformations, highlighting the library's zero-runtime nature.

import type { Equal, ExpectTrue, TupleToUnion } from 'typescript-type-utils';

// Example 1: Basic equality check at compile time
type StringIsString = ExpectTrue<Equal<string, string>>; // This type assertion will pass
// type StringIsNumber = ExpectTrue<Equal<string, number>>; // Uncommenting this line would cause a compile-time error

// Example 2: Using Equal for structural type comparison
interface User { id: number; name: string; }
type UserAlias = { id: number; name: string; };
type UserTypesAreEqual = ExpectTrue<Equal<User, UserAlias>>; // Asserts structural equality

// Example 3: Transforming tuple types to union types
type MyTuple = [1, 'hello', true];
type UnionFromTuple = TupleToUnion<MyTuple>; // UnionFromTuple will be 1 | "hello" | true

// The types ensure correctness at compile-time. No runtime code is executed.
console.log("All type assertions passed at compile time if there are no errors in your IDE/console.");

view raw JSON →