Add TypeScript Type Checking to CI/CD
Beginner8 min
Configure continuous integration to run TypeScript type checking on every pull request, catching type errors before they reach production.
Prerequisites
- -TypeScript project
- -GitHub repository (or other CI platform)
Steps
1
Create a GitHub Actions workflow
Add a workflow that runs type checking on every push and pull request.
$ mkdir -p .github/workflows && cat > .github/workflows/typecheck.yml << 'EOF'
name: Type Check
on:
push:
branches: [main]
pull_request:
branches: [main]
jobs:
typecheck:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: pnpm/action-setup@v4
with:
version: 9
- uses: actions/setup-node@v4
with:
node-version: 20
cache: pnpm
- run: pnpm install --frozen-lockfile
- run: pnpm tsc --noEmit
EOF
2
Add a type-check npm script
Add a dedicated script that CI and developers can use consistently.
$ npm pkg set scripts.typecheck='tsc --noEmit'
3
Add a pre-commit hook with Husky
Run type checking locally before commits reach CI.
$ pnpm add -D husky && npx husky init && echo 'pnpm typecheck' > .husky/pre-commit
For faster local feedback, use lint-staged to only type-check changed files, though tsc --noEmit checks the whole project regardless.
4
Verify the CI workflow runs correctly
Push the workflow file and check that it triggers on the next PR.
$ git add .github/workflows/typecheck.yml && git status
Full Script
FAQ
Discussion
Loading comments...