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
Why Test Types
expectTypeOf from Vitest
import { expectTypeOf } from 'vitest';
expectTypeOf(42).toBeNumber();
expectTypeOf('hello').toBeString();Asserting Return Types
import { identity } from '../src/utils';
expectTypeOf(identity(42)).toEqualTypeOf<number>();
expectTypeOf(identity('hi')).toEqualTypeOf<string>();tsd Library
npm install --save-dev tsdtsd Assertions
import { expectType, expectError } from 'tsd';
import { add } from '../src';
expectType<number>(add(1, 2));
expectError(add('a', 'b'));expect-type Library
import { expectTypeOf } from 'expect-type';
expectTypeOf<User>().toHaveProperty('name').toBeString();Testing Conditional Types
type IsArray<T> = T extends any[] ? true : false;
expectTypeOf<IsArray<string[]>>().toEqualTypeOf<true>();
expectTypeOf<IsArray<string>>().toEqualTypeOf<false>();Testing Mapped Types
type Mutable = { -readonly [K in keyof ReadonlyUser]: ReadonlyUser[K] };
expectTypeOf<Mutable['name']>().toBeString();
expectTypeOf<Mutable>().not.toMatchTypeOf<Readonly<Mutable>>();Running tsd in CI
// package.json
{ "scripts": { "test:types": "tsd" } }Type Tests vs Runtime Tests
Quick Check
Recap
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
- Jest Setup for TypeScript with ts-jest
- Writing Type-Safe Unit Tests
- Mocking Modules and Functions in TypeScript
- Type Testing with expectTypeOf and tsd