tsd: Check TypeScript Type Definitions

0.33.0 · active · verified Sun Apr 19

tsd is a utility designed for testing TypeScript type definitions, enabling developers to verify the correctness of their `.d.ts` files. The current stable version is 0.33.0, and the project maintains a relatively frequent release cadence, often updating to support newer TypeScript versions shortly after their release. It distinguishes itself by performing static analysis on `.test-d.ts` files, interpreting special assertion functions like `expectType`, `expectError`, and `expectAssignable` to check type compatibility without executing runtime code. This approach ensures that your type definitions accurately reflect your module's API and behavior, catching potential type-related regressions before they manifest as runtime errors or incorrect IDE IntelliSense. tsd is primarily used via its CLI, which automatically discovers project `package.json`, main type definition files, and test files within a configured directory.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to define a type, write basic `tsd` tests using `expectType`, `expectAssignable`, and `expectError` assertions, and how to test asynchronous operations.

// index.d.ts
declare const concat: {
  (value1: string, value2: string): string;
  (value1: number, value2: number): number;
};
export default concat;

// index.test-d.ts
import { expectType, expectAssignable, expectError } from 'tsd';
import concat from '.';

// Assert that 'concat' with strings returns a string
expectType<string>(concat('foo', 'bar'));

// Assert that 'concat' with numbers returns a number
expectType<number>(concat(1, 2));

// Assert that 'concat' result is assignable to a union type (looser check)
expectAssignable<string | number>(concat('test', 'example'));

// Assert that 'concat' with incorrect types (e.g., booleans) throws a type error
expectError(concat(true, false));

// Example demonstrating top-level await with promises
async function testAsyncOperations() {
  const asyncProcess = async (input: string): Promise<string> => Promise.resolve(`Processed: ${input}`);
  expectType<Promise<string>>(asyncProcess('data'));
  expectType<string>(await asyncProcess('data'));
}
testAsyncOperations(); // Execute the async test wrapper

view raw JSON →