Sub topic 1
What linting does and why TypeScript needs typescript-eslint
Prerequisites
This chapter assumes that you can read basic TypeScript, have a tsconfig.json file, and can run commands with npm and Node.js. You should also recognize ES module syntax such as import and export.
What linting checks
Linting is the automatic inspection of source code for patterns that may indicate bugs, unclear code, or inconsistent decisions. A linter reports these patterns before the program runs. Some rules are stylistic, such as requiring braces. Others protect behavior, such as detecting an unreachable statement.
ESLint is a JavaScript and TypeScript linting framework. It reads source files, builds a representation of their syntax, and runs rules against that representation. A rule can report a problem, suggest a fix, or apply a safe automatic fix.
Linting does not replace the TypeScript compiler. The compiler checks types and produces JavaScript or type declarations. ESLint checks broader coding patterns and can enforce decisions that the compiler does not make.
function formatUser(name: string, age: number): string {
if (age >= 18) {
return `${name} is an adult`;
}
return `${name} is a minor`;
}A compiler can confirm that name is a string and age is a number. A linter can additionally enforce rules such as avoiding unnecessary conditions, requiring a particular naming style, or prohibiting unused variables.
Why plain ESLint is not enough
TypeScript adds syntax that standard JavaScript parsers do not understand, including type annotations, interfaces, enums, and type-only imports. ESLint therefore needs a parser that can read TypeScript source.
Reading TypeScript syntax is only the first part. Some useful rules also need TypeScript's type information. For example, a syntax-only linter can see that a function call exists, but it cannot reliably determine whether that call returns a Promise. A type-aware rule can detect an unhandled promise and report a possible asynchronous bug.
typescript-eslint connects TypeScript to ESLint. It provides:
- A parser that converts TypeScript source into a form ESLint can inspect.
- ESLint rules designed for TypeScript.
- Preset configurations that group useful rules.
- Optional type-aware rules that use information from your TypeScript project.
The relationship looks like this:
Syntax-aware and type-aware linting
Syntax-aware linting works without loading the complete TypeScript program. It can catch issues such as unused variables, confusing syntax, and prohibited patterns. It is usually faster and simpler to configure.
Type-aware linting asks TypeScript for semantic information. Semantic information describes what declarations and values mean, not only how they are written. This enables rules that detect unsafe assignments, floating promises, and invalid type-related patterns.
For example, no-floating-promises can identify a promise that is created but never awaited or handled:
async function saveRecord(): Promise<void> {
// Save a record.
}
async function run(): Promise<void> {
saveRecord(); // A type-aware rule can report this.
}The compiler may allow this code because the call is type-correct. The operation can still fail without being handled, so a type-aware lint rule adds a useful safety check.
The practical model
Think of the tools as complementary:
| Tool | Main question |
|---|---|
| TypeScript compiler | Are the types and program relationships valid? |
| ESLint | Does the code follow useful correctness and maintenance rules? |
| typescript-eslint | How can ESLint understand and check TypeScript? |
In later configuration, you will choose presets, set rule severity, and decide which files need type-aware checking. That separation lets a project apply fast checks broadly while reserving expensive checks for code that benefits from them.
Exercise
Why can a TypeScript project use both the compiler and ESLint instead of choosing one?
Show answer
The compiler checks type correctness and program relationships, while ESLint checks coding patterns and can use typescript-eslint for TypeScript-specific and type-aware rules.