Sub topic 16
Keep it typed: linting, tests and CI
Assume you have a working npm project, a tsconfig.json, and a converted file that compiles locally. This chapter shows how to keep the migration safe after the first successful build.
Separate the checks
TypeScript, a linter, and tests find different problems:
| Check | Finds | Example |
|---|---|---|
| Type checking | Invalid types and missing properties | Passing `undefined` where a `string` is required |
| Linting | Suspicious or inconsistent code | An unused variable |
| Tests | Incorrect behavior | Returning the wrong user name |
A successful type check does not prove that the program behaves correctly. Passing tests does not prove that every value has a safe type. Run all three checks.
Add separate scripts to package.json:
{
"scripts": {
"build": "tsc",
"typecheck": "tsc --noEmit",
"lint": "eslint .",
"test": "node --test",
"check": "npm run typecheck && npm run lint && npm test"
}
}The build script creates the JavaScript output in the directory configured by outDir. The typecheck script checks the project without creating files. Use typecheck in pull requests when another build step produces the deployable files.
Keep tests beside the migration
Convert a file in small steps, then run its tests before moving to the next file. Tests give you a behavior boundary: you can change annotations and implementation details while checking that the public behavior remains the same.
For example, this test checks a converted function:
import test from "node:test";
import assert from "node:assert/strict";
import { formatUser } from "./formatUser.js";
test("formats a user's name and age", () => {
assert.equal(
formatUser({ name: "Ada", age: 36 }),
"ADA (36)"
);
});If your TypeScript compiler emits CommonJS or a different directory, adjust the import to match the emitted module. Keep that module choice consistent in local commands and CI.
Run the same checks in CI
Continuous integration (CI) is an automated service that runs checks for each pull request. A GitHub Actions workflow can use the same command you run locally:
name: checks
on:
pull_request:
push:
branches: [main]
jobs:
verify:
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 check
- run: npm run buildnpm ci installs the exact versions recorded in package-lock.json. Commit that lockfile so local and CI installations use the same dependencies. Require the workflow to pass before merging.
Increase coverage as strictness grows
When you enable strict, old JavaScript assumptions often appear as errors. Keep the checks active while fixing them. Do not disable the entire type checker to silence one difficult file. Instead, isolate an unsafe boundary, use unknown, and validate the value there.
A practical migration sequence is:
- Run
typecheckon every change. - Enable lint rules for unused values and unsafe shortcuts.
- Add tests around converted behavior.
- Enable strict compiler options in small groups.
- Make CI run the same commands on every pull request.
Exercise
Why should a project run type checking, linting, and tests as separate checks?
Show answer
Each check finds a different class of problem: types find invalid assumptions, linting finds suspicious code patterns, and tests find incorrect runtime behavior.