This TypeScript compiler error occurs when you declare and assign a variable but never reference it in your code. Enable noUnusedLocals in tsconfig.json to catch these issues early and keep your codebase clean.
This error appears when TypeScript's `noUnusedLocals` compiler option is enabled and detects variables that are assigned values but never referenced anywhere in the code. The compiler flags these unused variables to help maintain clean, efficient code by removing unnecessary variable declarations. This diagnostic (TS6133) is classified as an error rather than a warning, which means your build will fail until you address it. While this might seem strict during development, it helps prevent dead code from accumulating in your project and ensures that every variable serves a purpose. The error commonly appears in several scenarios: unused function parameters, variables from destructuring assignments where you only need some values, temporary debugging variables left in the code, or refactored code where variables are no longer needed but weren't cleaned up.
If the variable truly isn't needed, simply remove it from your code:
// Before - causes error
function calculateTotal(price: number, tax: number) {
const discount = 0.1; // Error: assigned but never used
return price + tax;
}
// After - fixed
function calculateTotal(price: number, tax: number) {
return price + tax;
}This is the cleanest solution when the variable serves no purpose.
For function parameters or destructured values you must keep but don't use, prefix them with an underscore to signal intentional non-use:
// Unused parameter - required for function signature
function handleClick(_event: MouseEvent, userId: string) {
console.log('User clicked:', userId);
// _event is intentionally unused but needed for signature
}
// Destructuring - ignoring some values
function getUserInfo(user: User) {
const { id, name, _email, _createdAt } = user;
return `User ${id}: ${name}`;
}The underscore prefix tells TypeScript and other developers that the variable is intentionally unused.
When destructuring objects and you only need specific properties, use the rest operator instead of naming unused properties:
// Before - causes error
function processUser(user: User) {
const { id, name, email, createdAt } = user;
// email and createdAt are unused
return { id, name };
}
// After - using rest operator
function processUser(user: User) {
const { id, name, ...rest } = user;
return { id, name };
// rest contains email, createdAt, etc. but no error
}
// Or just destructure what you need
function processUser(user: User) {
const { id, name } = user;
return { id, name };
}For exceptional cases where you need to keep an unused variable temporarily (like during debugging or gradual refactoring), disable the check for that line:
function debugFunction(data: any) {
// @ts-ignore - keeping for debugging
const debugValue = data.someComplexProperty;
return processData(data);
}Use this sparingly - it's better to actually use the variable or remove it. Consider this a temporary measure only.
If you want to disable this check project-wide (not recommended for production code), modify your tsconfig.json:
{
"compilerOptions": {
"noUnusedLocals": false,
"noUnusedParameters": false
}
}Note that disabling these options removes a helpful code quality check. It's better to fix individual instances than disable the entire rule.
If you need more granular control, use ESLint's @typescript-eslint/no-unused-vars rule instead:
// .eslintrc.json
{
"rules": {
"@typescript-eslint/no-unused-vars": [
"error",
{
"argsIgnorePattern": "^_",
"varsIgnorePattern": "^_",
"caughtErrorsIgnorePattern": "^_"
}
]
}
}This allows you to ignore variables starting with underscore while still catching truly unused variables.
Compiler Option Strictness: The noUnusedLocals diagnostic is classified as an error (not a warning), which is stricter than many developers expect. There's ongoing discussion in the TypeScript community about whether it should be a warning instead, allowing builds to succeed while still highlighting the issue (see GitHub issue #40058).
Per-File Disabling: Unlike ESLint, TypeScript doesn't support per-file disabling of noUnusedLocals through comments like // @ts-nocheck. You must either fix the individual instances, use @ts-ignore on specific lines, or modify the project-wide tsconfig.json.
React Import Special Case: In older TypeScript/React projects using JSX, the React import was often flagged as unused even though it was required for JSX transformation. This was resolved in React 17+ with the new JSX transform that doesn't require importing React.
Write-Only Variables: Note that TypeScript's noUnusedLocals checks if a variable's value is ever *read*, not just if it's written to. A variable that's assigned multiple times but never read will still trigger this error, helping catch potential logic bugs where you computed a value but forgot to use it.
Type parameter 'X' is not used in the function signature
How to fix "Type parameter not used in function signature" in TypeScript
Type parameter 'X' is defined but never used
How to fix "Type parameter is defined but never used" in TypeScript
Function expression requires a return type
Function expression requires a return type
Value of type 'string | undefined' is not iterable
How to fix "Value is not iterable" in TypeScript
Type 'undefined' is not assignable to type 'string'
How to fix "Type undefined is not assignable to type string" in TypeScript