Skip to content

Utility for type-checking only the specified TypeScript files using your project's configuration.

Notifications You must be signed in to change notification settings

Mearman/typecheck-files

Repository files navigation

typecheck-files

npm version GitHub

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-files
yarn add -D typecheck-files
pnpm add -D typecheck-files

Usage

With lint-staged

{
  "lint-staged": {
    "*.ts": [
      "eslint --fix",
      "typecheck-files"
    ]
  }
}

CLI

typecheck-files src/index.ts src/utils.ts

Programmatic 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

  1. Loads your project's tsconfig.json
  2. Parses compiler options
  3. Creates a TypeScript Program with only the specified files
  4. 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 --noEmit

Related 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.

About

Utility for type-checking only the specified TypeScript files using your project's configuration.

Topics

Resources

Stars

Watchers

Forks

Packages

No packages published

Contributors 3

  •  
  •  
  •