A tiny tool to run tsc
on specific files without ignoring tsconfig.json
, with workspace support.
Born out of real-world frustrations with pre-commit hooks in monorepo environments:
- nano-staged + eslint --fix: Too many conflicts when multiple files are being fixed simultaneously
- pnpm run test: Way too slow for just a simple commit - nobody wants to wait minutes for basic validation
- tsc --noEmit: Fast and reliable, but doesn't show which specific files have errors - debugging becomes a nightmare!
- tsc-files-workspace: Perfect solution for showing filenames with errors, but doesn't work in workspace/monorepo setups
This enhanced version detects workspace configurations automatically and ensures each file gets checked with its correct tsconfig.json
. Now you get fast type checking with clear error reporting and proper workspace support - perfect for pre-commit hooks!
- ✅ Run TypeScript compiler on specific files
- ✅ Respect
tsconfig.json
configuration - ✅ NEW: Automatic workspace detection
- ✅ NEW: Group files by their closest
tsconfig.json
- ✅ NEW: Process each workspace package separately
- ✅ Support for monorepo projects
- ✅ Backward compatible with original tsc-files
npm install tsc-files-workspace
# or
pnpm add tsc-files-workspace
# or
yarn add tsc-files-workspace
tsc-files-workspace src/file1.ts src/file2.ts
When used in a monorepo with workspaces, the tool automatically:
- Detects if you're in a workspace (looks for
package.json
withworkspaces
field) - Groups files by their closest
tsconfig.json
- Runs TypeScript compiler separately for each group
# In a monorepo, this will automatically group files by workspace packages
tsc-files-workspace packages/server/src/app.ts packages/client/src/main.ts packages/shared/src/utils.ts
You can still specify a specific tsconfig.json
to disable workspace detection:
tsc-files-workspace --project packages/server/tsconfig.json src/file1.ts src/file2.ts
- Starts from current directory and walks up the directory tree
- Looks for
package.json
files with aworkspaces
field - If found, enables workspace mode
For each TypeScript file:
- Finds the closest
tsconfig.json
by walking up from the file's directory - Groups files that share the same
tsconfig.json
- Creates temporary config files for each group
- Runs
tsc
separately for each group
my-monorepo/
├── package.json (with workspaces)
├── tsconfig.json
├── packages/
│ ├── server/
│ │ ├── tsconfig.json
│ │ └── src/
│ │ └── app.ts
│ ├── client/
│ │ ├── tsconfig.json
│ │ └── src/
│ │ └── main.ts
│ └── shared/
│ ├── tsconfig.json
│ └── src/
│ └── utils.ts
Running:
tsc-files-workspace packages/server/src/app.ts packages/client/src/main.ts packages/shared/src/utils.ts
Will:
- Group
packages/server/src/app.ts
withpackages/server/tsconfig.json
- Group
packages/client/src/main.ts
withpackages/client/tsconfig.json
- Group
packages/shared/src/utils.ts
withpackages/shared/tsconfig.json
- Run TypeScript compiler 3 times, once for each group
-
Accurate type checking: Each file is checked with its appropriate
tsconfig.json
- Faster CI/CD: Only check changed files while respecting workspace boundaries
- Better error reporting: Errors are reported in the context of the correct workspace package
- Monorepo friendly: Works seamlessly with Lerna, Rush, pnpm workspaces, npm workspaces, etc.
This tool works great with Husky for pre-commit hooks. Here's how to set it up:
# Install husky and tsc-files-workspace
pnpm install --save-dev husky tsc-files-workspace
# Initialize husky
pnpx husky init
Create .husky/pre-commit
:
#!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh"
# Get staged TypeScript files
STAGED_TS_FILES=$(git diff --cached --name-only --diff-filter=ACM | grep -E '\.(ts|tsx)$')
if [ -n "$STAGED_TS_FILES" ]; then
echo "Type checking staged TypeScript files..."
npx tsc-files-workspace $STAGED_TS_FILES --noEmit
if [ $? -ne 0 ]; then
echo "TypeScript type check failed. Please fix the errors before committing."
exit 1
fi
fi
For more advanced setups, combine with lint-staged:
npm install --save-dev lint-staged
Add to package.json
:
{
"lint-staged": {
"*.{ts,tsx}": [
"tsc-files-workspace --noEmit",
"eslint --fix",
"prettier --write"
]
}
}
Update .husky/pre-commit
:
pnpx lint-staged
- Fast type checking: Only checks changed files
- Workspace aware: Automatically uses correct tsconfig for each file
- Early error detection: Catches type errors before they reach CI/CD
- Consistent code quality: Ensures all commits pass type checking
- Fully backward compatible with original
tsc-files-workspace
- Works with any TypeScript version >= 3.0
- Supports all
tsc
command line options - Works with any workspace manager (pnpm, npm, yarn, lerna, etc.)
- Integrates seamlessly with Husky, lint-staged, and other Git hooks
MIT