Skip to content

Type tests

Relay’s routeParams<"/jobs/:id/retry"> type extracts { id: string } from a path. A refactor made it return { [k: string]: string }. Every call site still compiled, because that wider type accepts everything, and for two weeks params.idd typos passed the build. No runtime test can see this: the code is correct, the type is wrong.

Types need tests, and every Core exercise already used them: Expect<Equal<A, B>> and @ts-expect-error. This lesson opens the box and shows why Equal is written the strange way it is.

A type test is a type alias that only compiles when something holds. tsc runs it. Nothing runs at runtime, and bun test does not check it:

type Expect<T extends true> = T
type Ok = Expect<true>
// @ts-expect-error: Type 'false' does not satisfy the constraint 'true'
type Broken = Expect<false>

Expect is one constraint. All the work is in making a type that is exactly true when two types are the same, and false otherwise.

The first idea is mutual assignability:

type NaiveEqual<A, B> = [A] extends [B] ? ([B] extends [A] ? true : false) : false
// These are all "true", and none of them should be:
const anyIsOne: NaiveEqual<any, 1> = true
const readonlyIsMutable: NaiveEqual<{ readonly id: string }, { id: string }> = true

Assignable in both directions is not the same as identical. any is assignable to and from everything. TypeScript ignores readonly when checking assignability. A test built on this says “pass” for the exact bugs you wrote it to catch: any leaking out of a function, or a readonly lost on a public type.

The version without the tuple brackets, A extends B ? ..., is worse: it distributes over unions, so NaiveEqual<"a" | "b", "a"> answers with true | false.

The reliable trick compares two generic functions. TypeScript treats two conditional types as identical only if their check types and branches are identical, and it uses its internal identity relation, which sees any, readonly and optional keys:

type Equal<A, B> = (<T>() => T extends A ? 1 : 2) extends (<T>() => T extends B ? 1 : 2) ? true : false
type Expect<T extends true> = T
type ExpectFalse<T extends false> = T
type Cases = [
Expect<Equal<1, 1>>,
Expect<Equal<{ id: string }, { id: string }>>,
ExpectFalse<Equal<any, 1>>,
ExpectFalse<Equal<{ readonly id: string }, { id: string }>>,
ExpectFalse<Equal<readonly string[], string[]>>,
ExpectFalse<Equal<{ a: 1 } | { b: 2 }, { a: 1 }>>,
]

It is not magic, and the handbook does not document it. It is the definition used by most type-testing libraries, and it has one famous edge: Equal<{ a: 1 } & { b: 2 }, { a: 1; b: 2 }> is false, because an intersection is not the same type as the merged object. Write your expected type the way the code produces it.

Two types need their own detectors, because they break ordinary conditional types:

type IsAny<T> = 0 extends 1 & T ? true : false
type IsNever<T> = [T] extends [never] ? true : false
type A = IsAny<any> // true
type B = IsAny<unknown> // false
type C = IsNever<never> // true
type D = IsNever<undefined> // false

1 & any is any, and 0 is assignable to any: nothing else makes that true, not even unknown. For never, the brackets matter. T extends never ? ... distributes, and distributing over an empty union gives an empty result: never, not true. Wrapping both sides in a tuple switches that off, the same trick as in conditional types.

Both comments silence the error on the next line. They differ when the error goes away:

declare function sendTo(target: { url: string }): void
// @ts-expect-error: url must be a string
sendTo({ url: 42 })
// @ts-ignore <- silent forever, even after the call is fixed
sendTo({ url: "https://example.com" })

@ts-expect-error is itself an error when the next line is fine. That is what makes it a test: “this must NOT compile”, and the build breaks the day someone loosens the type so that it does. @ts-ignore can never fail, so it proves nothing. Write the reason after the colon, so the next reader knows what error you expect.

bun:test ships expectTypeOf, a chainable version of the same idea. It is type-only: at runtime every call does nothing, and only tsc reports a mismatch.

import { expectTypeOf, test } from "bun:test"
declare function parseJob(text: string): { id: string; attempt: number }
test("parseJob has the right shape", () => {
expectTypeOf(parseJob).parameter(0).toBeString()
expectTypeOf(parseJob("")).toEqualTypeOf<{ id: string; attempt: number }>()
expectTypeOf(parseJob("")).toHaveProperty("id")
})

A wrong expectation is reported by tsc, with a message such as a missing property (the exact wording varies). bun test still passes. That is the main trap: running only bun test in CI means your type tests never run. Keep tsc in the same script. Use expectTypeOf for readable one-offs and a hand-written Equal when you want zero dependencies or a test file the browser Playground can run.

The most useful type tests check what callers see, not the internals:

type Equal<A, B> = (<T>() => T extends A ? 1 : 2) extends (<T>() => T extends B ? 1 : 2) ? true : false
type Expect<T extends true> = T
declare function defineRoute<const P extends string>(path: P): { path: P }
const retry = defineRoute("/jobs/:id/retry")
export type KeepsLiteral = Expect<Equal<typeof retry.path, "/jobs/:id/retry">>
// @ts-expect-error: a number is not a path
defineRoute(42)

Test both directions: the happy inference (KeepsLiteral) and the call that must be rejected. Put them in *.test.ts files next to the code. Use export type so an unused alias is not reported as dead code.

Replace the naive helpers in the starter. Equal, IsAny, IsNever, Expect and ExpectFalse are graded by type tests, including the cases above: any, unions, readonly, and never. The failing lines are type errors, so look at the red squiggles in the test file.

  • A type test is an alias that only compiles when it is true. tsc checks it, bun test does not.
  • [A] extends [B] is not equality: any and readonly slip through.
  • The generic-function Equal uses TypeScript’s identity relation and tells those apart.
  • IsAny is 0 extends 1 & T. IsNever is [T] extends [never], with the brackets.
  • @ts-expect-error fails when the error disappears. @ts-ignore never fails.
  • expectTypeOf is type-only: keep tsc in the test script.