Stop bad commits before they happen. Automatically block Git commits if any TypeScript errors are detected in your staged files.
safe-ts-commit
is a production-ready CLI tool designed to protect your codebase by blocking Git commits if TypeScript type errors are present in your staged files.
Perfect for teams and solo devs who value code quality and type safety.
- ✅ Type-Safe Commits – Prevent buggy or broken commits due to TS errors.
- ⚡ Fast & Lightweight – Only checks staged
.ts
/.tsx
files. - 🎯 Zero Config – No extra setup required beyond install.
- 🔌 Husky-Ready – Integrates seamlessly as a Git pre-commit hook.
- 🎨 Readable Output – Colorful and actionable terminal messages.
- 🧠 Detects only staged TypeScript files
- 🚫 Aborts commit if errors are found
- ✅ Allows commit to proceed if everything passes
- 🧼 Supports filenames with spaces/special characters
- 🖍️ Clean and colorful CLI messages
- 🧪 One-line setup: Auto-installs Husky and configures pre-commit hook.
- Node.js v12+
- TypeScript installed locally (
npm install typescript --save-dev
) - Git (with initialized repo)
npm install --save-dev safe-ts-commit
Don’t want to manually configure Husky? Run this:
npx safe-ts-commit --install
This will:
- ✅ Install Husky (if not already installed)
- ✅ Add
prepare
script to yourpackage.json
- ✅ Create a
.husky/pre-commit
hook that runssafe-ts-commit
- ✅ Lock your commits behind type safety in seconds
Perfect for teams or quick setups. One command and you’re protected.
- Install Husky:
npm install husky --save-dev
npx husky install
- Add a
prepare
script in yourpackage.json
:
"scripts": {
"prepare": "husky install"
}
- Add a pre-commit hook:
npx husky add .husky/pre-commit "npx safe-ts-commit"
When a commit is attempted, safe-ts-commit
:
- Collects staged files via
git diff --cached --name-only
- Filters
.ts
and.tsx
files - Runs
tsc --noEmit
on those files - If any type errors are found:
- ❌ The commit is blocked with a detailed error report
- If all files pass:
- ✅ The commit proceeds as normal
You can run it manually without a Git hook:
npx safe-ts-commit
safe-ts-commit [options]
--install Run auto-setup with Husky
--debug Print detailed debug logs
--silent Hide output unless there's an error
--tsconfig Path to custom tsconfig.json
Issue | Solution |
---|---|
TypeScript not found | Ensure you installed TypeScript locally (npm install typescript ) |
No files being checked | Make sure your TypeScript files are staged (git add path/to/file.ts ) |
Hook not running | Ensure Husky is installed and the pre-commit hook is executable |
safe-ts-commit/
├── bin/
│ └── cli.js # CLI entry (compiled from src/cli.ts)
├── src/
│ ├── getStagedFiles.ts
│ ├── checkTsErrors.ts
│ └── utils.ts
├── .husky/ # Husky hooks
├── tsconfig.json
├── package.json
└── README.md
MIT — feel free to fork, improve, and contribute!