How to fix Label 'X' is not referenced in TypeScript

TypeScriptBEGINNERLOWUpdated Dec 11, 2025· 92 views

The TypeScript compiler error "Label 'X' is not referenced" occurs when you declare a label in your code but never use it with break or continue statements. This warning helps catch potential bugs where you might have intended to write an object literal but accidentally created a label instead.

Quick fix

Fixes Label 'X' is not referenced

// Before: Using labels for nested loops
outerLoop: for (let i = 0; i < 10; i++) {
  innerLoop: for (let j = 0; j < 10; j++) {
    if (condition) {
      break outerLoop;
    }
  }
}

// After: Extract to a function
function processNestedLoops() {
  for (let i = 0; i < 10; i++) {
    for (let j = 0; j < 10; j++) {
      if (condition) {
        return; // Exit the function instead of using break with label
      }
    }
  }
}
  1. Check if the label is actually needed
  2. Add the missing break or continue statement
  3. Fix object literal syntax errors
  4. Remove unused labels
typescript — error
// Before: Using labels for nested loops
outerLoop: for (let i = 0; i < 10; i++) {
innerLoop: for (let j = 0; j < 10; j++) {
if (condition) {
break outerLoop;
}
}
}
// After: Extract to a function
function processNestedLoops() {
for (let i = 0; i < 10; i++) {
for (let j = 0; j < 10; j++) {
if (condition) {
return; // Exit the function instead of using break with label
}
}
}
}
Label 'X' is not referenced

What this error means

In TypeScript and JavaScript, labels are identifiers that can be placed before statements to create named points in your code. They are primarily used with break and continue statements to control flow in nested loops or blocks. When you declare a label but never reference it, TypeScript warns you because this is usually a mistake—either you forgot to use the label, or you accidentally wrote a label when you meant to write something else (like an object literal). Labels are a rarely used feature in modern JavaScript/TypeScript, so when they appear unused, it often indicates a syntax error. For example, you might have intended to write `{ verified: true }` but accidentally wrote `verified: true` without the curly braces, creating a label instead of an object.

Typical symptoms

  • TypeScript compiler shows error TS7028: "Unused label."
  • Your IDE shows a warning about an unused label
  • Code compiles but with warnings about unreferenced labels
  • The label appears in your code but is never used with break or continue

Common causes

  1. Accidentally creating a label when you meant to write an object literal (missing curly braces)
  2. Forgetting to use a declared label with break or continue statements
  3. Removing break/continue statements that referenced a label but forgetting to remove the label declaration
  4. Copy-pasting code that includes labels but not the corresponding control flow statements

How to fix "Label 'X' is not referenced"

1Check if the label is actually needed

First, determine if you intended to use a label at all. Labels are rarely needed in modern JavaScript/TypeScript. Consider if you can refactor your code to avoid using labels entirely.

typescript
// Before: Using labels for nested loops
outerLoop: for (let i = 0; i < 10; i++) {
  innerLoop: for (let j = 0; j < 10; j++) {
    if (condition) {
      break outerLoop;
    }
  }
}

// After: Extract to a function
function processNestedLoops() {
  for (let i = 0; i < 10; i++) {
    for (let j = 0; j < 10; j++) {
      if (condition) {
        return; // Exit the function instead of using break with label
      }
    }
  }
}
2Add the missing break or continue statement

If you actually need the label, make sure you reference it with a break or continue statement.

typescript
// Error: Label declared but not used
myLabel: for (let i = 0; i < 10; i++) {
  console.log(i);
  // Missing: break myLabel or continue myLabel
}

// Fixed: Add the break statement
myLabel: for (let i = 0; i < 10; i++) {
  console.log(i);
  if (i === 5) {
    break myLabel; // Now the label is referenced
  }
}
3Fix object literal syntax errors

A common mistake is writing label: value when you meant { label: value }. Check if you accidentally created a label instead of an object literal.

typescript
// Error: This creates a label "verified" with value true
function verifyAge(age: number) {
  if (age > 18) {
    verified: true; // Creates a label, not an object!
  }
}

// Fixed: Add curly braces to create an object literal
function verifyAge(age: number) {
  if (age > 18) {
    return { verified: true }; // Returns an object
  }
}
4Remove unused labels

If the label is truly unnecessary, simply remove it from your code.

typescript
// Before: Unused label
unusedLabel: for (let i = 0; i < 10; i++) {
  console.log(i);
}

// After: Label removed
for (let i = 0; i < 10; i++) {
  console.log(i);
}
5Configure TypeScript compiler options

If you want to disable this warning, you can configure the allowUnusedLabels compiler option in your tsconfig.json file.

json
// tsconfig.json
{
  "compilerOptions": {
    "allowUnusedLabels": true, // Disables the unused label warning
    // Other options...
  }
}

Note: It's generally better to fix the actual issue rather than disabling the warning, as unused labels often indicate bugs.

6Verify the fix

After making changes, run the TypeScript compiler to ensure the error is resolved:

bash
npx tsc --noEmit

Or check your IDE/editor to see if the warning has disappeared. Make sure your code still behaves as expected, especially if you removed or modified labels that were part of control flow logic.

Advanced notes

The allowUnusedLabels compiler option was introduced in TypeScript 1.8. When set to false, TypeScript will raise error TS7028 for any unused labels. When set to true or undefined (default), TypeScript provides warnings but doesn't fail compilation. This option works alongside allowUnreachableCode to help catch common JavaScript syntax errors.

Labels in JavaScript/TypeScript are different from labels in other languages like C or Go. They only work with break and continue statements—there is no goto statement in JavaScript. Labels cannot be used with function declarations in strict mode, and attempting to do so will result in a different error: "SyntaxError: functions cannot be labelled."

Frequently asked questions

What does "Label 'X' is not referenced" mean?
In TypeScript and JavaScript, labels are identifiers that can be placed before statements to create named points in your code. They are primarily used with break and continue statements to control flow in nested loops or blocks. When you declare a label but never reference it, TypeScript warns you because this is usually a mistake—either you forgot to use the label, or you accidentally wrote a label when you meant to write something else (like an object literal).
How do I fix "Label 'X' is not referenced"?
The TypeScript compiler error "Label 'X' is not referenced" occurs when you declare a label in your code but never use it with break or continue statements. This warning helps catch potential bugs where you might have intended to write an object literal but accidentally created a label instead.
What causes "Label 'X' is not referenced"?
Accidentally creating a label when you meant to write an object literal (missing curly braces) Forgetting to use a declared label with break or continue statements Removing break/continue statements that referenced a label but forgetting to remove the label declaration
Is the "Label 'X' is not referenced" error serious?
It is a low-severity error — more of a blocker than a risk. The steps above resolve it.

Official resources & further reading