Sub topic 5
Tuning ESLint Rules: Severity and Options
This chapter assumes you know basic TypeScript, npm, ES modules, and the ESLint flat config format. It also assumes that typescript-eslint is already installed and that your project has an eslint.config.mjs file.
A lint rule checks for one kind of problem. For example, @typescript-eslint/no-explicit-any reports uses of the any type. ESLint lets you decide how strongly each rule should be enforced and, when supported, how the rule should behave.
Choosing a rule severity
A rule can have one of three severity levels:
| Setting | Meaning | Typical use |
|---|---|---|
| `"off"` or `0` | Do not run the rule | An intentional exception |
| `"warn"` or `1` | Report the problem but allow the command to pass | Gradual adoption |
| `"error"` or `2` | Report the problem and fail the lint command | Required project policy |
String values are usually easier to read. Numeric values are useful when generating configuration programmatically.
You set severities in the rules object. The rule name is the key, and the severity is the value.
// eslint.config.mjs
import eslint from '@eslint/js';
import tseslint from 'typescript-eslint';
import { defineConfig } from 'eslint/config';
export default defineConfig(
eslint.configs.recommended,
tseslint.configs.recommended,
{
rules: {
'@typescript-eslint/no-explicit-any': 'error',
'@typescript-eslint/consistent-type-imports': 'warn',
'no-console': 'off',
},
},
);This configuration requires explicit alternatives to any, reports inconsistent type imports as warnings, and disables the core no-console rule.
A warning still deserves attention. However, a warning normally produces a successful exit status, so it does not block a commit or CI job unless you add stricter command options or configure your CI policy.
Passing rule options
Some rules accept options. To provide them, use an array. The first item is the severity. Later items are rule-specific options.
For example, @typescript-eslint/consistent-type-imports can require imports used only for types to use the import type syntax.
import tseslint from 'typescript-eslint';
import { defineConfig } from 'eslint/config';
export default defineConfig(
tseslint.configs.recommended,
{
rules: {
'@typescript-eslint/consistent-type-imports': [
'error',
{
prefer: 'type-imports',
fixStyle: 'separate-type-imports',
},
],
},
},
);With this setting, write type-only imports like this:
import type { User } from './models.js';
import { createUser } from './users.js';The exact option names and allowed values differ by rule. Check the rule documentation before adding options. A configuration can load successfully but still fail validation if an option has the wrong shape or value.
Understanding configuration order
Flat config uses an array of configuration objects. When multiple objects apply to a file, later settings generally override earlier settings for the same rule.
If a preset marks a rule as a warning, a later object can promote it to an error. If you disable a rule later, the rule will not report violations for matching files.
Selecting a practical policy
Use errors for rules that protect correctness, such as unsafe promise handling or unreachable code. Use warnings while introducing a rule into an existing codebase. Turn a rule off only when the rule conflicts with a deliberate project decision or a dependency constraint.
Avoid enabling every available rule immediately. Start with a recommended preset, then add a small number of rules that address real mistakes in your codebase. Review new rules as part of normal dependency upgrades because presets and rule behavior can change.
Exercise
What is the difference between this configuration and ['warn', { prefer: 'type-imports' }] for the same rule?
Show answer
The configuration uses the severity error, so a violation fails the lint command. The other configuration reports a warning and supplies the rule option prefer: 'type-imports'.