Typing Tests
Typing tests means using TypeScript inside unit tests (so mocks and fixtures are checked) and writing tests of types (so a utility's result is the type you intended). They are different jobs.
Detailed Explanation
Most "TypeScript tests" are ordinary tests compiled with tsc or tsx / Vitest / Jest. The gain is that a fixture { id: 1 } will not assign to User if id is a string. The test file is part of the type project (include your *.test.ts or use a tsconfig that extends the app config). If tests are excluded, you get runtime coverage and no type coverage.
That is the first job: type the system under test, type the fakes, and let the checker reject illegal setups. UserRepo as an interface makes a fake one object literal. as User in a test is a smell; it is how an illegal fixture compiles.
The second job is tests that fail the build when a type is wrong. A runtime expect(typeof x).toBe("string") does not see UserId vs string. Tools for this:
expectTypeOf(Vitest) /expectTypeOffromexpect-typetsdassertions in*.test-d.tsfiles (expectType,expectError)// @ts-expect-erroron the next line, which must be an error ortscfailssatisfiesin a test file as a cheap assert
@ts-expect-error is the one you can use without a library. It is also easy to place on the wrong line, where a different error silences it. Prefer a typed assertion helper that names the type.
Keep type tests next to the utility they pin (partial.test-d.ts next to partial.ts). They should not boot an app. They should instantiate the generic with a few representative Ts, including never, unions, and optionals — those are the cases mapped types get wrong.
Do not replace runtime tests with type tests. A correct Promise<User> type can still resolve to undefined. Do not skip type tests for a public generic utility; a wrong Omit equivalent will compile in every app until someone notices the extra key.
isolatedModules and transpileOnly in a test runner will not run tsc. If your CI only runs Jest with isolatedModules: true, type errors in tests never fail the build. Run tsc --noEmit as well.
Code Examples
1. A runtime test that is still typed.
// 1. A runtime test that is still typed.
type User = { id: string; email: string };
function domain(user: User): string {
return user.email.split("@")[1] ?? "";
}
const ada: User = { id: "u1", email: "ada@example.com" };
if (domain(ada) !== "example.com") throw new Error("domain");2. @ts-expect-error pins a forbidden call.
// 2. @ts-expect-error pins a forbidden call.
function onlyString(s: string): string {
return s;
}
// @ts-expect-error — number is not a string
onlyString(1);3. A type-level assert without a library.
// 3. A type-level assert without a library.
type Equal<A, B> =
(<T>() => T extends A ? 1 : 2) extends (<T>() => T extends B ? 1 : 2) ? true : false;
type _ok = Equal<Pick<{ a: 1; b: 2 }, "a">, { a: 1 }>;
const _assert: _ok = true;Common Gotchas
`transpileOnly` / `isolatedModules` skip type-checking
CI must run `tsc --noEmit` or a type-aware test runner.
`as User` in a fixture disables the first job
write a satisfies or a helper `user(partial): User`.
Type tests do not execute the function
a matching type can still throw. Keep both kinds.