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