0Pricing
TypeScript Academy · Lesson

Type Testing with expectTypeOf and tsd

Assert type correctness alongside runtime behavior.

Type Testing with expectTypeOf and tsd is a free TypeScript Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the TypeScript Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Welcome

Testing types themselves — not just runtime behavior — ensures your TypeScript utilities and generic types work correctly.

Why Test Types

Generic utilities and conditional types can have bugs that only affect certain type arguments. Type tests catch these.

expectTypeOf from Vitest

Vitest provides expectTypeOf for compile-time type assertions.
import { expectTypeOf } from 'vitest';
expectTypeOf(42).toBeNumber();
expectTypeOf('hello').toBeString();

Asserting Return Types

Assert the return type of a function.
import { identity } from '../src/utils';
expectTypeOf(identity(42)).toEqualTypeOf<number>();
expectTypeOf(identity('hi')).toEqualTypeOf<string>();

tsd Library

tsd is a CLI tool for type testing TypeScript declaration files.
npm install --save-dev tsd

tsd Assertions

Write .test-d.ts files with tsd assertions.
import { expectType, expectError } from 'tsd';
import { add } from '../src';
expectType<number>(add(1, 2));
expectError(add('a', 'b'));

expect-type Library

The expect-type library provides Jest-compatible type assertions.
import { expectTypeOf } from 'expect-type';
expectTypeOf<User>().toHaveProperty('name').toBeString();

Testing Conditional Types

Verify conditional types produce the correct output for various inputs.
type IsArray<T> = T extends any[] ? true : false;
expectTypeOf<IsArray<string[]>>().toEqualTypeOf<true>();
expectTypeOf<IsArray<string>>().toEqualTypeOf<false>();

Testing Mapped Types

Ensure mapped type utilities produce correct shapes.
type Mutable = { -readonly [K in keyof ReadonlyUser]: ReadonlyUser[K] };
expectTypeOf<Mutable['name']>().toBeString();
expectTypeOf<Mutable>().not.toMatchTypeOf<Readonly<Mutable>>();

Running tsd in CI

Add tsd to your CI pipeline to catch type regressions.
// package.json
{ "scripts": { "test:types": "tsd" } }

Type Tests vs Runtime Tests

Type tests run at compile time (zero runtime cost). Runtime tests validate behavior. Both are necessary.

Quick Check

What does `expectTypeOf(add(1, 2)).toEqualTypeOf()` verify?

Recap

Type Testing with expectTypeOf and tsd: mastered the key concepts and patterns.

Frequently asked questions

Is the “Type Testing with expectTypeOf and tsd” lesson free?

Yes — the full text of “Type Testing with expectTypeOf and tsd” is free to read here on the web, and the TypeScript Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the TypeScript Academy course, upgrade to CoddyKit PRO.

What will I learn in “Type Testing with expectTypeOf and tsd”?

Assert type correctness alongside runtime behavior. You practise TypeScript Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start TypeScript Academy?

No prior experience is required. TypeScript Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Type Testing with expectTypeOf and tsd” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this TypeScript Academy lesson?

Yes. Every TypeScript Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Jest Setup for TypeScript with ts-jest
  2. Writing Type-Safe Unit Tests
  3. Mocking Modules and Functions in TypeScript
  4. Type Testing with expectTypeOf and tsd
← Back to TypeScript Academy