typescript-compare

raw JSON →
0.0.2 verified Sat Apr 25 auth: no javascript

Typescript-compare is a utility library providing TypeScript generic types for comparing types at compile time. Version 0.0.2 offers type-level comparison operators (e.g., eq, lt, gt) and helpers like compare and strictEqual. It enables conditional types based on type equality, ordering, and more, useful for building complex type functions. The library is lightweight, with no runtime dependencies, and is intended for advanced TypeScript type programming. It is released on an as-needed cadence.

error error TS2304: Cannot find name 'eq'.
cause Missing or incorrect import statement.
fix
Make sure to import eq as a named import: import { eq } from 'typescript-compare'
error error TS2307: Cannot find module 'typescript-compare' or its corresponding type declarations.
cause Package not installed or TypeScript cannot resolve it.
fix
Run npm install typescript-compare --save-dev and ensure tsconfig.json includes node_modules.
error error TS2554: Expected 1 arguments, but got 2.
cause Using compare or eq incorrectly on runtime values instead of types.
fix
Use type syntax: type Result = eq<A, B>; not eq(A, B);
gotcha Only works at compile time; no runtime values are exported.
fix Use only in type contexts (e.g., in type aliases, conditional types).
deprecated The package may be considered experimental and not widely used.
fix Consider alternatives like ts-essentials or type-fest for more stable type utilities.
gotcha TypeScript version compatibility: requires TypeScript 2.4+.
fix Ensure TypeScript version is at least 2.4.
npm install typescript-compare
yarn add typescript-compare
pnpm add typescript-compare

Shows basic usage of type comparison functions eq, compare, and strictEqual for compile-time type checking.

import { eq, compare, strictEqual } from 'typescript-compare'

// Check if two types are equal
type TestEqual = eq<number, number> // true
type TestNotEqual = eq<number, string> // false

// Compare types for ordering (e.g., in conditional types)
type CompareResult = compare<number, string> // -1 | 0 | 1 (depending on type order)

// Strict equality (no coercion)
type Strict = strictEqual<true, true> // true
type NotStrict = strictEqual<true, boolean> // false