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 that must be true
Section titled “A type test is a type that must be true”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 naive Equal is wrong
Section titled “The naive Equal is wrong”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> = trueconst readonlyIsMutable: NaiveEqual<{ readonly id: string }, { id: string }> = trueAssignable 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 exact Equal
Section titled “The exact Equal”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 : falsetype Expect<T extends true> = Ttype 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.
IsAny and IsNever
Section titled “IsAny and IsNever”Two types need their own detectors, because they break ordinary conditional types:
type IsAny<T> = 0 extends 1 & T ? true : falsetype IsNever<T> = [T] extends [never] ? true : false
type A = IsAny<any> // truetype B = IsAny<unknown> // falsetype C = IsNever<never> // truetype D = IsNever<undefined> // false1 & 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.
@ts-expect-error, not @ts-ignore
Section titled “@ts-expect-error, not @ts-ignore”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 stringsendTo({ url: 42 })
// @ts-ignore <- silent forever, even after the call is fixedsendTo({ 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.
expectTypeOf in bun
Section titled “expectTypeOf in bun”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.
Testing the inferred types of an API
Section titled “Testing the inferred types of an API”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 : falsetype 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 pathdefineRoute(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.
Exercise
Section titled “Exercise”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.
tscchecks it,bun testdoes not. [A] extends [B]is not equality:anyandreadonlyslip through.- The generic-function
Equaluses TypeScript’s identity relation and tells those apart. IsAnyis0 extends 1 & T.IsNeveris[T] extends [never], with the brackets.@ts-expect-errorfails when the error disappears.@ts-ignorenever fails.expectTypeOfis type-only: keeptscin the test script.
Further reading
Section titled “Further reading”- TypeScript 3.9 release notes: ts-expect-error comments: what the directive does.
- TypeScript 3.9 release notes: ts-ignore or ts-expect-error?: when to choose which.