@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
Usage
Import the utilities you need in your TypeScript files:Available Utilities
Assertion Utilities
- Expect - Generic type assertion wrapper
- ExpectTrue - Assert a type resolves to
true - ExpectFalse - Assert a type resolves to
false - ExpectExtends - Assert a value type extends an expected type
- ExpectValidArgs - Assert function arguments are valid
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
- Debug - Expand type for better readability
- MergeInsertions - Recursively merge object intersections
- UnionToIntersection - Convert union types to intersection types
Type Testing Pattern
The standard pattern for type testing in Type Challenges is to create a type alias containing a tuple of test cases:Error Testing
You can test that certain type operations should produce errors using the@ts-expect-error directive: