Check TypeScript type definitions with static analysis and comprehensive assertion functions
—
Command-line interface for testing entire projects with file discovery and configuration support. The CLI provides an easy way to run type definition tests from the command line or in CI/CD pipelines.
tsd [path] [--typings file] [--files pattern] [--show-diff] [--help] [--version]The CLI searches for a package.json file in the current or specified directory, finds type definition files, locates test files, and runs them through the TypeScript compiler for static analysis.
Specify the type definition file to test.
# Test specific typings file
tsd --typings dist/api.d.ts
# Short form
tsd -t dist/api.d.tsDefault behavior: Uses the types property from package.json, or falls back to index.d.ts.
Specify test files with glob patterns. Can be used multiple times.
# Single pattern
tsd --files "test-d/**/*.ts"
# Multiple patterns
tsd --files "test-d/**/*.ts" --files "src/**/*.test-d.tsx"
# Short form
tsd -f "**/*.test-d.ts"Default behavior: Searches for *.test-d.ts and *.test-d.tsx files in the project root or test-d directory.
Display type error differences between expected and received types.
tsd --show-diffThis shows detailed diff output when type assertions fail, making it easier to understand type mismatches.
Display help information.
tsd --helpDisplay version information.
tsd --version# Test current directory
tsd
# Test specific directory
tsd /path/to/my-project
# Test with verbose diff output
tsd --show-diff# Test specific typings file
tsd --typings lib/index.d.ts
# Test files in multiple directories
tsd --files "types/**/*.test-d.ts" --files "src/**/*.test-d.ts"
# Test TypeScript JSX files
tsd --files "**/*.test-d.tsx"{
"scripts": {
"test:types": "tsd",
"test:types:diff": "tsd --show-diff",
"test:api-types": "tsd --typings dist/api.d.ts --files api-tests/*.test-d.ts"
}
}# GitHub Actions
- name: Test Type Definitions
run: npm run test:types
# Or directly
- name: Test Type Definitions
run: npx tsd.PHONY: test-types
test-types:
npx tsd --show-diff
.PHONY: test-api-types
test-api-types:
npx tsd --typings dist/api.d.ts --files "api-tests/**/*.test-d.ts"The CLI follows a specific order of operations:
# Locates package.json in current or specified directory
tsd /path/to/project # Looks for /path/to/project/package.json
tsd # Looks for ./package.jsonError if not found: No package.json file found in <path>. Make sure you are running the command in a Node.js project.
# Priority order:
# 1. --typings flag value
# 2. "types" field in package.json
# 3. "typings" field in package.json
# 4. Same name as "main" field with .d.ts extension
# 5. index.d.tsExample package.json:
{
"name": "my-package",
"main": "dist/index.js",
"types": "dist/index.d.ts"
}# Without --files flag, searches for:
# 1. <typings-file-name>.test-d.ts
# 2. <typings-file-name>.test-d.tsx
# 3. Files in test directory (default: test-d/)
# Example: if typings file is "dist/api.d.ts"
# Looks for: api.test-d.ts, api.test-d.tsx, test-d/**/*.ts, test-d/**/*.tsx{
"name": "my-package",
"tsd": {
"directory": "type-tests"
}
}# Uses custom directory instead of default "test-d"
tsd # Will look in type-tests/ directory$ tsd
# No output when all tests pass (exit code 0)$ tsd
index.test-d.ts
✖ 10:20 Argument of type 'string' is not assignable to parameter of type 'number'.
$ echo $?
1$ tsd --show-diff
index.test-d.ts
✖ 10:20 Argument of type 'string' is not assignable to parameter of type 'number'.
- Expected
+ Received
- number
+ string$ tsd
api.test-d.ts
✖ 5:15 Property 'foo' does not exist on type 'ApiResponse'.
utils.test-d.ts
⚠ 12:8 Type 'deprecated' is deprecated.
✖ 15:10 Expected 2 arguments, but got 3.Note: Warnings do not cause non-zero exit codes.
Default configuration applied by TSD:
{
"strict": true,
"jsx": "react",
"target": "es2020",
"lib": ["es2020", "dom", "dom.iterable"],
"module": "commonjs",
"esModuleInterop": true,
"noUnusedLocals": false,
"moduleResolution": "node",
"skipLibCheck": false
}{
"name": "my-package",
"tsd": {
"directory": "my-test-dir",
"compilerOptions": {
"strict": false,
"target": "es2015"
}
}
}TSD respects tsconfig.json in the project root:
{
"compilerOptions": {
"strict": true,
"exactOptionalPropertyTypes": true
}
}Note: moduleResolution and skipLibCheck options cannot be overridden.
# Missing package.json
$ tsd /invalid/path
Error: No `package.json` file found in `/invalid/path`. Make sure you are running the command in a Node.js project.
# Missing type definition file
$ tsd --typings missing.d.ts
Error: The type definition `missing.d.ts` does not exist at `/current/path/missing.d.ts`. Is the path correct? Create one and try again.
# No test files found
$ tsd
Error: The test file `index.test-d.ts` or `index.test-d.tsx` does not exist in `/current/path`. Create one and try again.
# No test files matching pattern
$ tsd --files "nonexistent/**/*.ts"
Error: Could not find any test files with the given pattern(s). Create one and try again.# Check what files TSD is finding
ls -la *.test-d.ts test-d/
# Verify package.json configuration
cat package.json | grep -A 5 '"tsd"'
# Check TypeScript configuration
cat tsconfig.json
# Run with explicit paths for debugging
tsd --typings dist/index.d.ts --files "test-d/**/*.test-d.ts"# Test specific package in monorepo
tsd packages/api
tsd packages/utils --typings dist/index.d.ts
# Test all packages
for pkg in packages/*; do
echo "Testing $pkg"
tsd "$pkg"
done# Only test if type files exist
if [ -f "dist/index.d.ts" ]; then
tsd --typings dist/index.d.ts
else
echo "No type definitions found, skipping tests"
fi# .git/hooks/pre-commit
#!/bin/bash
echo "Running type definition tests..."
if ! tsd; then
echo "Type tests failed! Commit aborted."
exit 1
fiInstall with Tessl CLI
npx tessl i tessl/npm-tsd