Type Tests
tsd is a tool that type-checks *.test-d.ts files and asserts about types: expectType, expectError, expectAssignable. It does not run your functions. It runs the type checker on purpose.
Detailed Explanation
A unit test calls a function and looks at a value. A type test instantiates a type and looks at the result. tsd is built for the second. You write a file that imports your library's types (and sometimes values, only to take typeof) and you write assertions that tsc would not make on its own.
import { expectType, expectError } from "tsd";
import { pick } from "./pick.js";
const user = { id: "u1", email: "a@b.c" };
expectType<string>(pick(user, "id"));
expectError(pick(user, "nope"));
expectType<T>(value) fails the tsd run if value is not exactly T (or assignable, depending on the helper). expectError(expr) fails if that expression is not an error. That is how you pin "this call must stay illegal."
tsd uses your tsconfig (or a tsd section in package.json) and typically checks against the published .d.ts if you point it at dist. Library authors use it so a change to a mapped type cannot ship with a silent widening. Application authors use it for shared utilities the same way.
Related tools: expect-type, Vitest expectTypeOf, eslint with type-aware rules. tsd is the one that popularized *.test-d.ts next to a package's types. You do not need tsd if you already run a typecheck project that includes those files and you are happy with @ts-expect-error plus Equal<A, B> helpers.
tsd will not catch a function that returns the right type and the wrong value. It will not execute pick. Pair it with runtime tests. It will not type-check files you did not include. Point it at the declaration files you ship, not only at src, if emit rewrites paths.
Keep type tests small. One file per utility. Cases: a typical T, a union, an optional property, readonly, never. Those are the four ways mapped types regress.
Code Examples
1. expectType and expectError (tsd style).
// 1. expectType and expectError (tsd style).
import { expectType, expectError } from "tsd";
declare function pick<T, K extends keyof T>(obj: T, key: K): T[K];
const user = { id: "u1", email: "a@b.c" };
expectType<string>(pick(user, "id"));
expectError(pick(user, "missing"));2. Without tsd — @ts-expect-error plus a compile of this file.
// 2. Without tsd — @ts-expect-error plus a compile of this file.
declare function pick<T, K extends keyof T>(obj: T, key: K): T[K];
const user = { id: "u1" };
const id: string = pick(user, "id");
// @ts-expect-error
pick(user, "missing");3. Point tsd at emitted types for a library.
// 3. Point tsd at emitted types for a library.
{
"tsd": {
"directory": "test-d"
}
}Common Gotchas
Testing `src` and shipping `dist` can diverge
run `tsd` against the `.d.ts` you publish.
These files are not executed
a `console.log` in a `.test-d.ts` does nothing in `tsd`.
Exact vs assignable
`expectType` vs `expectAssignable` are different. `string` is assignable from `"ok"`; they are not the same type.