typecheck-files
v1.0.4
Published
Typecheck specific TypeScript files using your project's tsconfig.json
Maintainers
Readme
typecheck-files
Typecheck specific TypeScript files using your project's tsconfig.json.
Why
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.
Installation
npm install -D typecheck-filesyarn add -D typecheck-filespnpm add -D typecheck-filesUsage
With lint-staged
{
"lint-staged": {
"*.ts": [
"eslint --fix",
"typecheck-files"
]
}
}CLI
typecheck-files src/index.ts src/utils.tsProgrammatic API
import { 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)
}How it works
- 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.).
Limitations
- 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 --noEmitRelated Packages
tsc-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.
