no-unused-vars
Disallow unused variables.
Extending "plugin:@typescript-eslint/recommended"
in an ESLint configuration enables this rule.
This is an "extension" rule that replaces a core ESLint rule to work with TypeScript. See Rules > Extension Rules.
This rule extends the base no-unused-vars
rule from ESLint core. It adds support for TypeScript features, such as types.
How to Use
- Flat Config
- Legacy Config
export default tseslint.config({
rules: {
// Note: you must disable the base rule as it can report incorrect errors
"no-unused-vars": "off",
"@typescript-eslint/no-unused-vars": "error"
}
});
module.exports = {
"rules": {
// Note: you must disable the base rule as it can report incorrect errors
"no-unused-vars": "off",
"@typescript-eslint/no-unused-vars": "error"
}
};
Try this rule in the playground ↗
Options
See eslint/no-unused-vars
's options.
FAQs
What benefits does this rule have over TypeScript?
TypeScript provides noUnusedLocals
and noUnusedParameters
compiler options that can report errors on unused local variables or parameters, respectively.
Those compiler options can be convenient to use if you don't want to set up ESLint and typescript-eslint.
However:
- These lint rules are more configurable than TypeScript's compiler options.
- For example, the
varsIgnorePattern
option can customize what names are always allowed to be exempted. TypeScript hardcodes its exemptions to names starting with_
. If you would like to emulate the TypeScript style of exempting names starting with_
, you can use this configuration (this includes errors as well):{
"rules": {
"@typescript-eslint/no-unused-vars": [
"error",
{
"args": "all",
"argsIgnorePattern": "^_",
"caughtErrors": "all",
"caughtErrorsIgnorePattern": "^_",
"destructuredArrayIgnorePattern": "^_",
"varsIgnorePattern": "^_",
"ignoreRestSiblings": true
}
]
}
}
- For example, the
- ESLint can be configured within lines, files, and folders. TypeScript compiler options are linked to their TSConfig file.
- Many projects configure TypeScript's reported errors to block builds more aggressively than ESLint complaints. Blocking builds on unused variables can be inconvenient.
We generally recommend using @typescript-eslint/no-unused-vars
to flag unused locals and parameters instead of TypeScript.
Editors such as VS Code will still generally "grey out" unused variables even if noUnusedLocals
and noUnusedParameters
are not enabled in a project.
Also see similar rules provided by ESLint:
Why does this rule report variables used only for types?
This rule does not count type-only uses when determining whether a variable is used. Declaring variables only to use them for types adds code and runtime complexity. The variables are never actually used at runtime. They can be misleading to readers of the code.
- typeof Variables
- Zod Schemas
For example, if a variable is only used for typeof
, this rule will report:
const box = {
// ~~~
// 'box' is assigned a value but only used as a type.
value: 123,
};
export type Box = typeof box;
Open in PlaygroundInstead, it's often cleaner and less code to write out the types directly:
export interface Box {
value: number;
}
Open in PlaygroundFor example, if a Zod schema variable is only used for typeof
, this rule will report:
import { z } from 'zod';
const schema = z.object({
// ~~~~~~
// 'schema' is assigned a value but only used as a type.
value: z.number(),
});
export type Box = z.infer<typeof schema>;
Open in PlaygroundInstead, it's often cleaner and less code to write out the types directly:
export interface Box {
value: number;
}
Open in PlaygroundIf you find yourself writing runtime values only for types, consider refactoring your code to declare types directly.