Skip to main content
The @type-challenges/utils package provides a comprehensive set of type-level utilities for testing and validating TypeScript types. These utilities enable you to write type assertions that are evaluated at compile time.

Installation

npm install -D @type-challenges/utils

Usage

Import the utilities you need in your TypeScript files:
import type { Equal, Expect, ExpectTrue, ExpectFalse } from '@type-challenges/utils'

type cases = [
  Expect<Equal<MyType, ExpectedType>>,
  ExpectTrue<SomeCondition>,
  ExpectFalse<AnotherCondition>,
]

Available Utilities

Assertion Utilities

Comparison Utilities

  • Equal - Deep equality check between two types
  • NotEqual - Inverse of Equal
  • Alike - Structural equality with merged intersections

Type Detection

  • IsAny - Detect if a type is any
  • NotAny - Inverse of IsAny
  • IsTrue - Assert a type is true
  • IsFalse - Assert a type is false

Advanced Utilities

Type Testing Pattern

The standard pattern for type testing in Type Challenges is to create a type alias containing a tuple of test cases:
import type { Equal, Expect } from '@type-challenges/utils'

type cases = [
  Expect<Equal<Result1, Expected1>>,
  Expect<Equal<Result2, Expected2>>,
  // ... more test cases
]
If any assertion fails, TypeScript will produce a compile-time error, indicating which test case failed.

Error Testing

You can test that certain type operations should produce errors using the @ts-expect-error directive:
// @ts-expect-error - This should fail
MyType<InvalidArgument>