Typecheck specific TypeScript files using your project's tsconfig.json.
I wanted to type-check only the staged files with lint-staged.
Unfortunately passing specific files like tsc --noEmit file1.ts file2.ts will cause TypeScript to simply ignore your tsconfig.json.
This tool uses TypeScript's public API to typecheck specific files while preserving all compiler options from your tsconfig.
npm install -D typecheck-filesyarn add -D typecheck-filespnpm add -D typecheck-files{
"lint-staged": {
"*.ts": [
"eslint --fix",
"typecheck-files"
]
}
}typecheck-files src/index.ts src/utils.tsimport { typecheckFiles } from "typecheck-files"
const result = typecheckFiles(["src/index.ts", "src/utils.ts"])
if (!result.success) {
console.error("Type errors found:")
for (const error of result.errors) {
console.error(` ${error.file}:${error.line}:${error.character}: ${error.message}`)
}
process.exit(1)
}- Loads your project's
tsconfig.json - Parses compiler options
- Creates a TypeScript Program with only the specified files
- Reports errors from those files
Unlike tsc file.ts, this preserves all your compiler options (paths, strict mode, etc.).
- Only typechecks the specified files
- Files that import from the specified files are not typechecked
- This is a tradeoff for speed—you get fast commits, but type errors in dependent files may be deferred
For full coverage, run a complete typecheck in CI:
tsc --noEmittsc-files solves the same problem using a different approach: it generates a temporary tsconfig and spawns a separate tsc process. This package uses TypeScript's Program API directly, avoiding subprocess overhead and temporary files.
If you're looking for similar tools for other parts of your workflow, check out test-related for running only tests related to changed files.