Lesson 4 of 7
Type-Level Testing with `expectTypeOf`
Beyond testing runtime behaviour, sometimes you want to test that TypeScript infers the correct types - especially for utility types, generic functions, and complex type transformations. Vitest ships `expectTypeOf` for this. For Jest, `tsd` or inline `satisfies` assertions provide similar guarantees.
Type Testing - expectTypeOf & satisfies
Type Testing - expectTypeOf & satisfies
// ── Option 1: Vitest expectTypeOf ────────────────────────────
import { expectTypeOf } from "vitest";
describe("Type-level tests", () => {
it("getProperty returns the correct type", () => {
const task = { id: 1, title: "Test", status: "todo" as const };
expectTypeOf(getProperty(task, "id")).toEqualTypeOf<number>();
expectTypeOf(getProperty(task, "title")).toEqualTypeOf<string>();
expectTypeOf(getProperty(task, "status")).toEqualTypeOf<"todo">();
});
it("DeepPartial makes all properties optional", () => {
type Input = { a: string; b: { c: number } };
type Output = DeepPartial<Input>;
expectTypeOf<Output>().toEqualTypeOf<{ a?: string; b?: { c?: number } }>();
});
});
// ── Option 2: satisfies - inline type assertions ───────────────
// This compiles → the type is correct.
// This fails to compile → the type is wrong → caught by tsc --noEmit
const labels = {
todo: "📌 To Do",
"in-progress": "⚡ In Progress",
done: "✅ Done",
archived: "📥 Archived",
} satisfies Record<TaskStatus, string>;
// If TaskStatus gets a new member "cancelled", this line errors → test-like behaviour.
// ── Option 3: Manual type assertions with never ───────────────
// Exhaustiveness tests - compile error if a case is missing:
function assertNever(x: never): never { throw new Error("Not exhaustive"); }
function describeStatus(status: TaskStatus): string {
switch (status) {
case "todo": return "Not started";
case "in-progress": return "In progress";
case "done": return "Complete";
case "archived": return "Archived";
default: return assertNever(status);
// If TaskStatus adds "cancelled", the default becomes an error
}
}
// ── Option 4: tsd - DefinitelyTyped testing tool ─────────────
// npm install --save-dev tsd
// Create <file>.test-d.ts:
// import { getProperty } from "./utils";
// import { expectType } from "tsd";
// expectType<number>(getProperty({ id: 1 }, "id"));
// expectType<string>(getProperty({ name: "" }, "name"));
// Run: npx tsdDiagram
Loading diagram…
Visual architecture and concepts for Type-Level Testing with `expectTypeOf`
Common Mistakes
- Only testing runtime, not types - utility types and generic functions can be 'correct at runtime' but have subtly wrong inferred types. Add type-level assertions for any generic utility you write.
- Over-testing obvious inference - `expectTypeOf(1 + 1).toEqualTypeOf<number>()` is noise. Focus type tests on non-obvious cases: generics, mapped types, conditional types, or any type you took time to design.
- Using `as` to silence type errors in tests - if you write `result as Task` to make a test compile, you've hidden a real type error under the cast. Fix the underlying type mismatch instead.
Key Takeaways
- Beyond testing runtime behaviour, sometimes you want to test that TypeScript infers the correct types - especially for utility types, generic functions, and complex type transformations.
- Only testing runtime, not types - utility types and generic functions can be 'correct at runtime' but have subtly wrong inferred types. Add type-level assertions for any generic utility you write.
- Over-testing obvious inference - `expectTypeOf(1 + 1).toEqualTypeOf<number>()` is noise. Focus type tests on non-obvious cases: generics, mapped types, conditional types, or any type you took time to design.
- Using `as` to silence type errors in tests - if you write `result as Task` to make a test compile, you've hidden a real type error under the cast. Fix the underlying type mismatch instead.
Finished this lesson?
0 of 7 in Module 11