Sub topic 13
Testing Type Utilities with Compile-Time Assertions
What compile-time assertions check
Assume you know TypeScript generics, conditional types, mapped types, tuples, and template literal types. This chapter focuses on verifying type-level programs. These programs produce types rather than JavaScript values, so ordinary runtime tests cannot check their results.
A compile-time assertion is a type expression that must satisfy a constraint. If the result is wrong, the TypeScript compiler reports an error.
A common pattern uses two helper types:
type Equal<X, Y> =
(<T>() => T extends X ? 1 : 2) extends
(<T>() => T extends Y ? 1 : 2)
? true
: false;
type Expect<T extends true> = T;Equal<X, Y> evaluates to true when two types are equivalent and false otherwise. Expect<T> accepts only true, because its type parameter is constrained with extends true.
You can now test a utility type:
type Reverse<
T extends unknown[],
Acc extends unknown[] = []
> = T extends [infer Head, ...infer Rest]
? Reverse<Rest, [Head, ...Acc]>
: Acc;
type TestReverse = Expect<
Equal<Reverse<[1, 2, 3]>, [3, 2, 1]>
>;If Reverse<[1, 2, 3]> is [3, 2, 1], TestReverse is valid. If the implementation returns [1, 2, 3], Equal becomes false, and Expect<false> fails.
Test the important branches
A useful test suite checks more than the main example. Test the base case, recursive case, unions, and unusual inputs.
type UnwrapPromise<T> =
T extends PromiseLike<infer U>
? UnwrapPromise<U>
: T;
type TestString = Expect<
Equal<UnwrapPromise<string>, string>
>;
type TestNested = Expect<
Equal<
UnwrapPromise<Promise<Promise<number>>>,
number
>
>;
type TestUnion = Expect<
Equal<
UnwrapPromise<Promise<string> | Promise<number>>,
string | number
>
>;The first assertion checks the base case. The second checks recursion. The third checks how the conditional type handles a union.
You can also test a negative result. For example, an array utility should reject a non-array type by returning never.
type ElementType<T> =
T extends (infer U)[] ? U : never;
type TestElement = Expect<
Equal<ElementType<boolean[]>, boolean>
>;
type TestNotArray = Expect<
Equal<ElementType<Date>, never>
>;| Case | Example | What it verifies | Column |
|---|---|---|---|
| Base case | `UnwrapPromise<string>` | Recursion stops correctly | |
| Nested input | `Promise<Promise<number>>` | Recursive branch works | |
| Invalid shape | `ElementType<Date>` | The fallback branch works | |
| Union input | `Promise<string> \ | Promise<number>` | Distribution is understood |
Run assertions with `tsc`
Assertions are checked when TypeScript compiles the file. They produce no JavaScript and have no runtime cost.
Save the examples in types.test.ts and run:
npx tsc --noEmit --strict types.test.tsA successful command means every assertion satisfied its constraint. To test that an assertion can detect a bug, temporarily change the expected result:
type FailingTest = Expect<
Equal<Reverse<[1, 2, 3]>, [1, 2, 3]>
>;The compiler reports that false does not satisfy the constraint true. Restore the correct expectation after confirming the test fails.
A simple testing workflow
Use this sequence:
- Identify each branch in the utility.
- Choose a small input for each branch.
- Assert the exact expected type.
- Include at least one recursive or union case when relevant.
- Run
tsc --noEmit --strict. - Read failures as evidence about the type computation.
Exercise
What does Expect<Equal<ElementType<number[]>, string>> do?
Show answer
It fails at compile time because ElementType<number[]> is number, so the equality check produces false instead of true.