TypeScript · Lesson 41 of 42
Linting, testing and building TypeScript projects
Add typescript-eslint to catch mistakes the compiler allows, test TypeScript with the built-in Node test runner, and build and check a project with npm scripts.
UNDERSTAND IT. THEN TRY IT.
What you can do after this lesson
You will know the small set of tools around the compiler that turn a TypeScript project into a dependable one: a linter, tests and a build, wired together with simple commands. None of them is exotic, and together they catch what the type system alone cannot.
Who checks what
It helps to be clear about each tool's job:
- The compiler checks that types fit together.
- A linter checks style and risky patterns the type system allows, such as a promise that is never awaited.
- Tests run your code and check that it behaves correctly.
- A formatter keeps the layout consistent, so reviews focus on meaning.
- A build tool turns your TypeScript into something that can run.
Each catches different mistakes, so they complement one another.
Linting with typescript-eslint
ESLint is the most widely used JavaScript linter, and the typescript-eslint project teaches it TypeScript. Install it as development dependencies, following the current instructions on the typescript-eslint website, then add a configuration file. This is a typical modern setup, shown for reading (the project's documentation is the reference):
// eslint.config.mjs
import { defineConfig, globalIgnores } from "eslint/config";
import eslint from "@eslint/js";
import tseslint from "typescript-eslint";
export default defineConfig(
globalIgnores(["dist"]),
eslint.configs.recommended,
tseslint.configs.recommended,
);globalIgnores(["dist"]) keeps the linter away from the JavaScript that tsc generates. Older guides use a function called tseslint.config(...), which the typescript-eslint project has since deprecated in favour of ESLint's own defineConfig. Tools that read your code through the TypeScript compiler's programming interface can lag behind new compiler versions, so typescript-eslint states which TypeScript versions it supports. If npm reports a version conflict when you install it, read the typescript-eslint documentation for the supported versions.
Add a script to run it, and the linter reports problems with the file and rule name. Rule names carry the plugin prefix, as in @typescript-eslint/no-explicit-any. Several rules matter most for TypeScript code:
@typescript-eslint/no-explicit-anyreports eachany, so the debt stays visible. It is in therecommendedconfiguration shown above.@typescript-eslint/no-floating-promisesreports a promise you neither await nor handle, a bug the compiler allows. It needs type information, so it is part of the "type-checked" configurations (such asrecommendedTypeChecked), which also need a little extra setup that the typescript-eslint documentation explains.@typescript-eslint/no-unused-varsfinds dead code. It is inrecommended.@typescript-eslint/no-non-null-assertionwarns about!, the shortcut from the assertions lesson. It is in thestrictconfiguration, not inrecommended.
Here is the kind of bug a linter catches that the compiler cannot:
async function save(): Promise<void> {
console.log("saving");
}
function main(): void {
save(); // promise ignored: failures would go unnoticed
console.log("finished");
}
main();saving
finishedThis compiles and runs, but finished prints before saving completes, and any error in save would vanish. The @typescript-eslint/no-floating-promises rule flags the line.
Testing
Tests check behaviour, which types cannot. A perfectly typed function can still return the wrong answer. Here is a function to test:
export function applyDiscount(price: number, percent: number): number {
if (percent < 0 || percent > 100) {
throw new RangeError("percent must be between 0 and 100");
}
return Math.round(price * (100 - percent)) / 100;
}
console.log(applyDiscount(200, 25));150TypeScript works with every common test runner. Node.js has a built-in one, so you can write a test with no extra packages. This test file is compiled with tsc like any other source file, then run with node --test. Because the test imports node:test, install @types/node and add "types": ["node"] to compilerOptions, as the lesson on declaration files explained:
import test from "node:test";
import assert from "node:assert/strict";
import { applyDiscount } from "./discount";
test("applies a percentage discount", () => {
assert.equal(applyDiscount(200, 25), 150);
});
test("rejects invalid percentages", () => {
assert.throws(() => applyDiscount(100, 150), RangeError);
});The default imports of node:test and node:assert/strict work with the nodenext configuration from the earlier lesson. In an older project that uses module: commonjs, the compiler may report that these modules can only be default-imported with the esModuleInterop flag; turn that flag on. The test runner is described in the Node.js documentation, which also explains how node --test finds test files.
Other runners, such as Vitest and Jest, are popular too, with their own advantages, like faster watching and richer mocking. Pick one that suits your project. Whatever the runner, test the edges: zero, negative values, empty lists and the invalid inputs a user could send.
Tests also benefit from types. When you change a function's signature, every test that calls it with the old shape fails to compile, so the tests and the code stay in step.
Building
How you build depends on what you are making:
- A Node.js program or library:
tsccompilessrctodist, and you run or publishdist. - A web application: a bundler or framework tool reads your TypeScript, removes the types and produces the files for the browser.
Many fast tools remove types without checking them. That is by design: it makes builds quick. The consequence is that a successful build does not prove your types are correct. Always run tsc --noEmit as a separate step.
npm scripts
Put the commands in package.json, so everyone, and your continuous integration system, runs the same thing:
{
"scripts": {
"check": "tsc --noEmit",
"lint": "eslint .",
"test": "tsc && node --test \"dist/**/*.test.js\"",
"build": "tsc",
"verify": "npm run check && npm run lint && npm test"
}
}Now npm run verify runs everything. Wire it into your continuous integration so every pull request is checked automatically, and a failing type, lint rule or test blocks the merge. Run the quick checks, check and lint, in your editor or on save, so you find problems when they are cheapest to fix.
Editor support
Your editor runs the same compiler, so errors appear as you type. Most editors can also run ESLint and show its findings inline, and format on save. Set these up once per project, and commit the editor settings that matter, such as the formatter, so the team shares them.
Common mistakes
Trusting the build to check types. Bundlers often strip types without checking. Run tsc --noEmit.
Using any to make lint errors disappear. Fix the types, or write a justified // eslint-disable-next-line comment.
Treating passing types as passing tests. Types check shapes. Tests check answers.
Leaving no single command. If checking takes five different commands, people skip some. Provide one verify script.
Ignoring warnings. A noisy linter gets ignored. Keep the rule set small and enforce it.
Exercise
Set up a small project with a source folder and these scripts: check, build, test and verify. Write the applyDiscount function and two tests using node:test. Add typescript-eslint, introduce a floating promise on purpose, and confirm the linter finds it. Finally, make a deliberate type error and see which scripts catch it.
KNOWLEDGE CHECK
A quick check. You’ve got this.
Try each question. Every answer comes with an explanation.
Ready for the next small win?
Mark the lesson complete when you are ready.