Sub topic 16
Catching Errors Before They Merge
Assume you know basic JavaScript modules, TypeScript annotations, interfaces, and union types. You also need Node.js, npm, and a terminal. This chapter shows how to make TypeScript checking part of the merge process.
Make the compiler a repeatable check
A local editor may show type errors, but editor settings differ between developers. Run the project’s TypeScript configuration directly:
npx tsc --noEmit --pretty falseThe --noEmit option checks the project without writing JavaScript files. The --pretty false option produces stable, plain-text output that is easier for CI logs and other tools to read. The command exits with status 0 when checking succeeds and a nonzero status when TypeScript reports an error.
Add the command to package.json so every developer and automation system uses the same check:
{
"scripts": {
"typecheck": "tsc --noEmit --pretty false",
"test": "node --test"
}
}Run it with:
npm run typecheckA typecheck is separate from tests. Tests check runtime behavior. TypeScript checks whether the code follows the declared static types. A project usually needs both.
Add typechecking to CI
Continuous integration (CI) is an automated service that runs checks for each branch or pull request. A minimal GitHub Actions workflow can install dependencies and run the typecheck:
name: Checks
on:
pull_request:
push:
branches: [main]
jobs:
typecheck:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 22
cache: npm
- run: npm ci
- run: npm run typechecknpm ci installs the exact dependency versions recorded in package-lock.json. This makes the CI environment more predictable than installing from a changed dependency range.
The workflow fails when npm run typecheck returns a nonzero status. Configure the repository’s branch protection rules to require this job before a pull request can merge. Otherwise, CI may report an error while the merge remains possible.
flowchart TD
A[Open pull request] --> B[CI installs locked dependencies]
B --> C[Run npm run typecheck]
C --> D{Any diagnostics?}
D -->|No| E[Required check passes]
D -->|Yes| F[Fix code and push again]
F --> B
E --> G[Merge is allowed]Keep CI aligned with local development
Use the same tsconfig.json, TypeScript version, and npm script locally and in CI. Avoid adding a separate CI-only flag that hides errors. If the repository has multiple configurations, name their scripts explicitly, such as typecheck:app and typecheck:tests.
| Check | Finds | Typical command |
|---|---|---|
| Typecheck | Static type errors | `npm run typecheck` |
| Tests | Runtime behavior errors | `npm test` |
| Build | Compilation and packaging errors | `npm run build` |
Exercise
Why should a pull request require a typecheck job instead of relying only on developers’ editors?
Show answer
Editors can use different settings or be misconfigured. A required CI job runs the repository’s compiler configuration consistently and blocks a merge when it fails.