TypeScript Best Practices

Chapter 1 · Practice 6 of 6

Run the type checker in CI, separate from the build

From Compiler settings and TypeScript 7

Why it matters

Many fast tools, including Node's type stripping, esbuild, SWC and most bundlers, remove types without checking them. A project can build and run while full of type errors. The only thing that checks types is the TypeScript compiler (or its language service in your editor).

Add a dedicated step that runs tsc --noEmit (or tsc -b with project references) and fails the pipeline on any error. Run it on every pull request, next to tests and lint. With TypeScript 7 this step is usually short enough that there is no reason to skip it.

Do

  • Add a typecheck script that runs tsc --noEmit and call it in CI
  • Fail the pipeline on type errors the same way you fail on test errors

Avoid

  • Assuming the bundler or node file.ts checked your types

Example

JSON
{
  "scripts": {
    "typecheck": "tsc --noEmit",
    "lint": "eslint .",
    "test": "vitest run"
  }
}