How to fix Label 'X' is not referenced in TypeScript
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
}
}
}
}- Check if the label is actually needed
- Add the missing break or continue statement
- Fix object literal syntax errors
- Remove unused labels
// Before: Using labels for nested loopsouterLoop: for (let i = 0; i < 10; i++) {innerLoop: for (let j = 0; j < 10; j++) {if (condition) {break outerLoop;}}}// After: Extract to a functionfunction 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
- 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
- Copy-pasting code that includes labels but not the corresponding control flow statements
How to fix "Label 'X' is not referenced"
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.
// 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
}
}
}
}If you actually need the label, make sure you reference it with a break or continue statement.
// 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
}
}A common mistake is writing label: value when you meant { label: value }. Check if you accidentally created a label instead of an object literal.
// 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
}
}If the label is truly unnecessary, simply remove it from your code.
// 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);
}If you want to disable this warning, you can configure the allowUnusedLabels compiler option in your tsconfig.json file.
// 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.
After making changes, run the TypeScript compiler to ensure the error is resolved:
npx tsc --noEmitOr 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
- 📚TypeScript Official Documentation
- 📖Typescriptlang.org - #allowUnusedLabels(Official Docs)
- 🔗
- 📖Typescriptlang.org - everyday-types.html(Official Docs)
- 🐙Github.com - 7028(GitHub)