Linting
Lint beyond types
The compiler proves types line up, but it stays silent about dead code, floating promises, and confusing style. A linter reads intent and flags patterns that often hide bugs.
Treat the two tools as partners. Types guard the contracts, lint guards the habits that keep code readable.
let discount = 0.2; discount = 0.25; console.log(discount);
0.25
ESLint with typescript-eslint
ESLint is the standard JavaScript linter, and the typescript-eslint project adds TypeScript aware parsing and rules on top of it. The command below is shown as text only so you can recognize it in docs.
Setup varies by project layout, so follow the typescript-eslint docs for your setup. No install step is shown here.
npx eslint .
A configured run prints a report of every finding with its file, line, and rule name; the exact output depends on the rules of the project.
Rules that matter
Start with a small set that prevents real bugs: no unused variables, no unsafe any leaks, and no forgotten await on promises. A short list that the team respects beats a long list that everyone ignores.
Add one rule at a time and fix the whole codebase before adding the next. Green builds stay green while standards rise step by step.
async function fetchName(id: number): Promise<string> { return "user-" + id; } async function loadUser(id: number): Promise<string> { return fetchName(id); } loadUser(7).then((name) => console.log(name));
user-7
Type-aware rules need a project
The most valuable rules ask the type checker for facts, such as whether a value may be null at this exact line. Those rules need the project config so the linter can load full type information.
Point the linter at tsconfig.json once. From then on it reasons with types, not just syntax, and its reports read like compiler errors with style advice.
tsc --noEmit
The command prints nothing when the types are clean, which leaves the linter free to report style and habits.
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What does a linter catch that the type checker does not?
Answer
Dead code, floating promises, and confusing style
Types prove the contracts; the linter reads intent and flags habits that often hide bugs.
-
What does the typescript-eslint project add to ESLint?
Answer
TypeScript aware parsing and rules
The project lets the linter read TypeScript and use type information in its rules.
-
Why do type-aware lint rules need the project config?
Answer
So the linter can load full type information
The rules ask the checker for facts, and the project file is what the checker reads.
-
How should a team add lint rules to an old codebase?
Answer
Add one rule at a time and fix the codebase before the next
A short list the team respects keeps the build green while the standards rise.
-
What does this program print?
let discount = 0.2; discount = 0.25; console.log(discount);Answer
0.25
The second assignment replaces the value, and the log prints the new one.
-
What does this program print?
async function fetchName(id: number): Promise<string> { return "user-" + id; } async function loadUser(id: number): Promise<string> { return fetchName(id); } loadUser(7).then((name) => console.log(name)); console.log("done");Answer
done / user-7
The synchronous done prints first, and the promise resolves to user-7 afterwards.
-
What does this program print?
console.log(Number("3") + 1);Answer
4
Number converts the text to 3, and the plus adds one.
-
What does this program print?
console.log([1, 2, 3].filter((n) => n > 1).length);Answer
2
Two elements are greater than one, so the filtered array has length 2.
-
What does the compiler say about this program?
async function loadUser(id: number): Promise<number> { return "x"; } loadUser(1).then((n) => console.log(n)); console.log("start");Answer
error TS2322: Type 'string' is not assignable to type 'number'.
The declared promise carries a number, and the returned string does not fit.
-
What does the compiler say about this program?
let x = 1; x = "two"; console.log(x);Answer
error TS2322: Type 'string' is not assignable to type 'number'.
The variable was inferred as number, and a string cannot be assigned to it.
Article author: Arthur Isaev