Course contentsShow
← TypeScript
Lesson 1213 of 1,79628. tsconfig in DepthFree lesson

allowUnusedLabels: Managing Label Usage

Control whether unused labels in code trigger compiler errors or are silently allowed.

allowUnusedLabels: Managing Label Usage

What you'll learn: How to control whether TypeScript flags unused labels in your code as errors or allows them silently.

What Are Labels in JavaScript/TypeScript?

Labels are a rarely-used JavaScript feature that let you name loops or code blocks, primarily for use with break and continue statements in nested loops:

outerLoop: for (let i = 0; i < 3; i++) {
  for (let j = 0; j < 3; j++) {
    if (i === 1 && j === 1) {
      break outerLoop; // Breaks out of the outer loop
    }
    console.log(i, j);
  }
}

The outerLoop: part is a label. It marks a specific location in your code that control flow statements can reference.

The Problem: Unused Labels

Sometimes developers create labels but forget to use them, or they remove the break/continue that referenced the label during refactoring. This leaves "dead" labels in the code:

myLabel: for (let i = 0; i < 5; i++) {
  console.log(i);
  // No break or continue references myLabel
}

These unused labels don't cause runtime errors, but they're code clutter and might indicate incomplete refactoring or a logic error.

The allowUnusedLabels Flag

The allowUnusedLabels compiler option controls TypeScript's behavior when it encounters labels that aren't referenced by any break or continue statement:

  • allowUnusedLabels: false (recommended): TypeScript emits an error for unused labels
  • allowUnusedLabels: true (default): Unused labels are silently allowed

In your tsconfig.json:

{
  "compilerOptions": {
    "allowUnusedLabels": false
  }
}

When set to false, the earlier example would trigger a compiler error, helping you catch potential mistakes.

When This Matters

Since labels are uncommon in modern JavaScript (most developers prefer structured approaches), you'll rarely encounter this. However, setting allowUnusedLabels: false adds an extra safety net for codebases that do use labels, ensuring they're intentional and functional.

Key Takeaway: Set allowUnusedLabels: false to catch unused labels as errors, preventing code clutter and potential logic mistakes in the rare cases where labels are used.