Skip to content

Commit 63a52cd

Browse files
Give a better message up-front for incompatible equality errors.
1 parent cb6325d commit 63a52cd

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

src/compiler/checker.ts

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20884,7 +20884,31 @@ namespace ts {
2088420884
}
2088520885

2088620886
function reportOperatorError() {
20887-
error(errorNode || operatorToken, Diagnostics.Operator_0_cannot_be_applied_to_types_1_and_2, tokenToString(operatorToken.kind), typeToString(leftType), typeToString(rightType));
20887+
let err = chainDiagnosticMessages(
20888+
/*elaboration*/ undefined,
20889+
Diagnostics.Operator_0_cannot_be_applied_to_types_1_and_2,
20890+
tokenToString(operatorToken.kind),
20891+
typeToString(leftType),
20892+
typeToString(rightType)
20893+
);
20894+
err = giveBetterPrimaryError(err);
20895+
20896+
diagnostics.add(createDiagnosticForNodeFromMessageChain(
20897+
errorNode || operatorToken,
20898+
err
20899+
));
20900+
}
20901+
20902+
function giveBetterPrimaryError(elaboration: DiagnosticMessageChain) {
20903+
switch (operatorToken.kind) {
20904+
case SyntaxKind.EqualsEqualsEqualsToken:
20905+
case SyntaxKind.EqualsEqualsToken:
20906+
return chainDiagnosticMessages(elaboration, Diagnostics.The_types_of_these_values_indicates_that_this_condition_will_always_be_0, "false");
20907+
case SyntaxKind.ExclamationEqualsEqualsToken:
20908+
case SyntaxKind.ExclamationEqualsToken:
20909+
return chainDiagnosticMessages(elaboration, Diagnostics.The_types_of_these_values_indicates_that_this_condition_will_always_be_0, "true");
20910+
}
20911+
return elaboration;
2088820912
}
2088920913
}
2089020914

src/compiler/diagnosticMessages.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1252,6 +1252,10 @@
12521252
"category": "Error",
12531253
"code": 2366
12541254
},
1255+
"The types of these values indicates that this condition will always be '{0}'.": {
1256+
"category": "Error",
1257+
"code": 2367
1258+
},
12551259
"Type parameter name cannot be '{0}'.": {
12561260
"category": "Error",
12571261
"code": 2368

0 commit comments

Comments
 (0)