Skip to content

Commit 6f06fd0

Browse files
Use a more direct error.
1 parent 2a19580 commit 6f06fd0

File tree

3 files changed

+20
-21
lines changed

3 files changed

+20
-21
lines changed

src/compiler/checker.ts

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20904,31 +20904,30 @@ namespace ts {
2090420904
}
2090520905

2090620906
function reportOperatorError() {
20907-
let err = chainDiagnosticMessages(
20908-
/*elaboration*/ undefined,
20909-
Diagnostics.Operator_0_cannot_be_applied_to_types_1_and_2,
20910-
tokenToString(operatorToken.kind),
20911-
typeToString(leftType),
20912-
typeToString(rightType)
20913-
);
20914-
err = giveBetterPrimaryError(err);
20915-
20916-
diagnostics.add(createDiagnosticForNodeFromMessageChain(
20917-
errorNode || operatorToken,
20918-
err
20919-
));
20907+
const leftStr = typeToString(leftType);
20908+
const rightStr = typeToString(rightType);
20909+
const errNode = errorNode || operatorToken;
20910+
if (!tryGiveBetterPrimaryError(errNode, leftStr, rightStr)) {
20911+
error(
20912+
errNode,
20913+
Diagnostics.Operator_0_cannot_be_applied_to_types_1_and_2,
20914+
tokenToString(operatorToken.kind),
20915+
leftStr,
20916+
rightStr,
20917+
);
20918+
}
2092020919
}
2092120920

20922-
function giveBetterPrimaryError(elaboration: DiagnosticMessageChain) {
20921+
function tryGiveBetterPrimaryError(errNode: Node, leftStr: string, rightStr: string) {
2092320922
switch (operatorToken.kind) {
2092420923
case SyntaxKind.EqualsEqualsEqualsToken:
2092520924
case SyntaxKind.EqualsEqualsToken:
20926-
return chainDiagnosticMessages(elaboration, Diagnostics.The_types_of_these_values_indicate_that_this_condition_will_always_be_0, "false");
20925+
return error(errNode, Diagnostics.This_condition_will_always_return_0_since_the_types_1_and_2_have_no_overlap, "false", leftStr, rightStr);
2092720926
case SyntaxKind.ExclamationEqualsEqualsToken:
2092820927
case SyntaxKind.ExclamationEqualsToken:
20929-
return chainDiagnosticMessages(elaboration, Diagnostics.The_types_of_these_values_indicate_that_this_condition_will_always_be_0, "true");
20928+
return error(errNode, Diagnostics.This_condition_will_always_return_0_since_the_types_1_and_2_have_no_overlap, "true", leftStr, rightStr);
2093020929
}
20931-
return elaboration;
20930+
return undefined;
2093220931
}
2093320932
}
2093420933

src/compiler/diagnosticMessages.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1252,7 +1252,7 @@
12521252
"category": "Error",
12531253
"code": 2366
12541254
},
1255-
"The types of these values indicate that this condition will always be '{0}'.": {
1255+
"This condition will always return '{0}' since the types '{1}' and '{2}' have no overlap.": {
12561256
"category": "Error",
12571257
"code": 2367
12581258
},

src/testRunner/unittests/tsserverProjectSystem.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5016,7 +5016,7 @@ namespace ts.projectSystem {
50165016
);
50175017
const errorResult = <protocol.Diagnostic[]>session.executeCommand(getErrRequest).response;
50185018
assert.isTrue(errorResult.length === 1);
5019-
assert.equal(errorResult[0].code, Diagnostics.The_types_of_these_values_indicate_that_this_condition_will_always_be_0.code);
5019+
assert.equal(errorResult[0].code, Diagnostics.This_condition_will_always_return_0_since_the_types_1_and_2_have_no_overlap.code);
50205020
});
50215021

50225022
it("should report semantic errors for configured js project with '// @ts-check' and skipLibCheck=true", () => {
@@ -5043,7 +5043,7 @@ namespace ts.projectSystem {
50435043
);
50445044
const errorResult = <protocol.Diagnostic[]>session.executeCommand(getErrRequest).response;
50455045
assert.isTrue(errorResult.length === 1);
5046-
assert.equal(errorResult[0].code, Diagnostics.The_types_of_these_values_indicate_that_this_condition_will_always_be_0.code);
5046+
assert.equal(errorResult[0].code, Diagnostics.This_condition_will_always_return_0_since_the_types_1_and_2_have_no_overlap.code);
50475047
});
50485048

50495049
it("should report semantic errors for configured js project with checkJs=true and skipLibCheck=true", () => {
@@ -5072,7 +5072,7 @@ namespace ts.projectSystem {
50725072
);
50735073
const errorResult = <protocol.Diagnostic[]>session.executeCommand(getErrRequest).response;
50745074
assert.isTrue(errorResult.length === 1);
5075-
assert.equal(errorResult[0].code, Diagnostics.The_types_of_these_values_indicate_that_this_condition_will_always_be_0.code);
5075+
assert.equal(errorResult[0].code, Diagnostics.This_condition_will_always_return_0_since_the_types_1_and_2_have_no_overlap.code);
50765076
});
50775077
});
50785078

0 commit comments

Comments
 (0)