tsc-files-workspace
v2.0.1
Published
A tiny tool to run tsc on specific files without ignoring tsconfig.json, with PNPM workspace support
Readme
tsc-files-workspace
A tiny tool to run tsc on specific files without ignoring tsconfig.json, with workspace support.
Why tsc-files-workspace?
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!
Features
- ✅ Run TypeScript compiler on specific files
- ✅ Respect
tsconfig.jsonconfiguration - ✅ 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
Installation
npm install tsc-files-workspace
# or
pnpm add tsc-files-workspace
# or
yarn add tsc-files-workspaceUsage
Basic Usage (same as original tsc-files-workspace)
tsc-files-workspace src/file1.ts src/file2.tsWorkspace Support (NEW)
When used in a monorepo with workspaces, the tool automatically:
- Detects if you're in a workspace (looks for
package.jsonwithworkspacesfield) - 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.tsManual Project Specification
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.tsHow It Works
Workspace Detection
- Starts from current directory and walks up the directory tree
- Looks for
package.jsonfiles with aworkspacesfield - If found, enables workspace mode
File Grouping
For each TypeScript file:
- Finds the closest
tsconfig.jsonby walking up from the file's directory - Groups files that share the same
tsconfig.json - Creates temporary config files for each group
- Runs
tscseparately for each group
Example Workspace Structure
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.tsRunning:
tsc-files-workspace packages/server/src/app.ts packages/client/src/main.ts packages/shared/src/utils.tsWill:
- Group
packages/server/src/app.tswithpackages/server/tsconfig.json - Group
packages/client/src/main.tswithpackages/client/tsconfig.json - Group
packages/shared/src/utils.tswithpackages/shared/tsconfig.json - Run TypeScript compiler 3 times, once for each group
Benefits
- 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.
Husky Integration
This tool works great with Husky for pre-commit hooks. Here's how to set it up:
Installation with Husky
# Install husky and tsc-files-workspace
pnpm install --save-dev husky tsc-files-workspace
# Initialize husky
pnpx husky initPre-commit Hook Example
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
fiAdvanced Husky Setup with Lint-staged
For more advanced setups, combine with lint-staged:
npm install --save-dev lint-stagedAdd to package.json:
{
"lint-staged": {
"*.{ts,tsx}": [
"tsc-files-workspace --noEmit",
"eslint --fix",
"prettier --write"
]
}
}Update .husky/pre-commit:
pnpx lint-stagedBenefits of Using with Husky
- 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
Compatibility
- Fully backward compatible with original
tsc-files-workspace - Works with any TypeScript version >= 3.0
- Supports all
tsccommand line options - Works with any workspace manager (pnpm, npm, yarn, lerna, etc.)
- Integrates seamlessly with Husky, lint-staged, and other Git hooks
License
MIT
