How to fix Variable 'x' is assigned a value but never used in TypeScript

TypeScriptBEGINNERLOWUpdated Dec 11, 2025ยท 225 views

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.

Quick fix

Fixes Variable 'x' is assigned a value but never used

// 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;
}
  1. Remove the unused variable if not needed
  2. Prefix unused variables with underscore
  3. Use rest operator to ignore destructured values
  4. Disable the check with ts-ignore comment
typescript โ€” error
// 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;
}
Variable 'x' is assigned a value but never used

What this error means

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.

Typical symptoms

  • Build fails with "Variable 'x' is assigned a value but never used" error
  • VS Code or IDE shows red underline on variable declarations
  • TypeScript compilation stops with TS6133 diagnostic code
  • Error occurs even when variable is initialized but never read
  • Warning appears for function parameters that are defined but unused

Common causes

  1. noUnusedLocals compiler option is enabled in tsconfig.json
  2. Variable is declared and assigned but never referenced in the code
  3. Function parameter is required for signature but not used in implementation
  4. Destructuring assignment includes variables you don't need
  5. Debugging variables or console.log statements were removed but variables remain
  6. Code was refactored but old variable declarations weren't cleaned up

How to fix "Variable 'x' is assigned a value but never used"

1Remove the unused variable if not needed

If the variable truly isn't needed, simply remove it from your code:

typescript
// 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.

2Prefix unused variables with underscore

For function parameters or destructured values you must keep but don't use, prefix them with an underscore to signal intentional non-use:

typescript
// 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.

3Use rest operator to ignore destructured values

When destructuring objects and you only need specific properties, use the rest operator instead of naming unused properties:

typescript
// 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 };
}
4Disable the check with ts-ignore comment

For exceptional cases where you need to keep an unused variable temporarily (like during debugging or gradual refactoring), disable the check for that line:

typescript
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.

5Adjust tsconfig.json compiler options

If you want to disable this check project-wide (not recommended for production code), modify your tsconfig.json:

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.

6Use ESLint configuration for more flexibility

If you need more granular control, use ESLint's @typescript-eslint/no-unused-vars rule instead:

json
// .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.

Advanced notes

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.

Frequently asked questions

What does "Variable 'x' is assigned a value but never used" mean?
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.
How do I fix "Variable 'x' is assigned a value but never used"?
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.
What causes "Variable 'x' is assigned a value but never used"?
noUnusedLocals compiler option is enabled in tsconfig.json Variable is declared and assigned but never referenced in the code Function parameter is required for signature but not used in implementation
Is the "Variable 'x' is assigned a value but never used" error serious?
It is a low-severity error โ€” more of a blocker than a risk. The steps above resolve it.

Official resources & further reading