Skip to content

Commit 950593b

Browse files
Merge pull request microsoft#25251 from Microsoft/betterAlwaysTrueFalseErrors
Better "always true/false" errors
2 parents 16f045b + 704cd7b commit 950593b

File tree

40 files changed

+1500
-739
lines changed

40 files changed

+1500
-739
lines changed

src/compiler/checker.ts

+25-1
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_indicate_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_indicate_that_this_condition_will_always_be_0, "true");
20910+
}
20911+
return elaboration;
2088820912
}
2088920913
}
2089020914

src/compiler/diagnosticMessages.json

+4
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 indicate 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

src/testRunner/unittests/tsserverProjectSystem.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -4997,7 +4997,7 @@ namespace ts.projectSystem {
49974997
assert.isTrue(error2Result.length === 0);
49984998
});
49994999

5000-
it("should report semanitc errors for loose JS files with '// @ts-check' and skipLibCheck=true", () => {
5000+
it("should report semantic errors for loose JS files with '// @ts-check' and skipLibCheck=true", () => {
50015001
const jsFile = {
50025002
path: "/a/jsFile.js",
50035003
content: `
@@ -5016,10 +5016,10 @@ 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.Operator_0_cannot_be_applied_to_types_1_and_2.code);
5019+
assert.equal(errorResult[0].code, Diagnostics.The_types_of_these_values_indicate_that_this_condition_will_always_be_0.code);
50205020
});
50215021

5022-
it("should report semanitc errors for configured js project with '// @ts-check' and skipLibCheck=true", () => {
5022+
it("should report semantic errors for configured js project with '// @ts-check' and skipLibCheck=true", () => {
50235023
const jsconfigFile = {
50245024
path: "/a/jsconfig.json",
50255025
content: "{}"
@@ -5043,10 +5043,10 @@ 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.Operator_0_cannot_be_applied_to_types_1_and_2.code);
5046+
assert.equal(errorResult[0].code, Diagnostics.The_types_of_these_values_indicate_that_this_condition_will_always_be_0.code);
50475047
});
50485048

5049-
it("should report semanitc errors for configured js project with checkJs=true and skipLibCheck=true", () => {
5049+
it("should report semantic errors for configured js project with checkJs=true and skipLibCheck=true", () => {
50505050
const jsconfigFile = {
50515051
path: "/a/jsconfig.json",
50525052
content: JSON.stringify({
@@ -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.Operator_0_cannot_be_applied_to_types_1_and_2.code);
5075+
assert.equal(errorResult[0].code, Diagnostics.The_types_of_these_values_indicate_that_this_condition_will_always_be_0.code);
50765076
});
50775077
});
50785078

tests/baselines/reference/api/tsserverlibrary.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -5151,6 +5151,7 @@ declare namespace ts {
51515151
The_left_hand_side_of_an_assignment_expression_must_be_a_variable_or_a_property_access: DiagnosticMessage;
51525152
Operator_0_cannot_be_applied_to_types_1_and_2: DiagnosticMessage;
51535153
Function_lacks_ending_return_statement_and_return_type_does_not_include_undefined: DiagnosticMessage;
5154+
The_types_of_these_values_indicate_that_this_condition_will_always_be_0: DiagnosticMessage;
51545155
Type_parameter_name_cannot_be_0: DiagnosticMessage;
51555156
A_parameter_property_is_only_allowed_in_a_constructor_implementation: DiagnosticMessage;
51565157
A_rest_parameter_must_be_of_an_array_type: DiagnosticMessage;

tests/baselines/reference/capturedLetConstInLoop5.errors.txt

+8-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
tests/cases/compiler/capturedLetConstInLoop5.ts(174,13): error TS2365: Operator '==' cannot be applied to types '0' and '1'.
2-
tests/cases/compiler/capturedLetConstInLoop5.ts(229,13): error TS2365: Operator '==' cannot be applied to types '0' and '1'.
1+
tests/cases/compiler/capturedLetConstInLoop5.ts(174,13): error TS2367: The types of these values indicate that this condition will always be 'false'.
2+
Operator '==' cannot be applied to types '0' and '1'.
3+
tests/cases/compiler/capturedLetConstInLoop5.ts(229,13): error TS2367: The types of these values indicate that this condition will always be 'false'.
4+
Operator '==' cannot be applied to types '0' and '1'.
35

46

57
==== tests/cases/compiler/capturedLetConstInLoop5.ts (2 errors) ====
@@ -178,7 +180,8 @@ tests/cases/compiler/capturedLetConstInLoop5.ts(229,13): error TS2365: Operator
178180
(() => x + v);
179181
if (x == 1) {
180182
~~~~~~
181-
!!! error TS2365: Operator '==' cannot be applied to types '0' and '1'.
183+
!!! error TS2367: The types of these values indicate that this condition will always be 'false'.
184+
!!! error TS2367: Operator '==' cannot be applied to types '0' and '1'.
182185
return;
183186
}
184187
}
@@ -235,7 +238,8 @@ tests/cases/compiler/capturedLetConstInLoop5.ts(229,13): error TS2365: Operator
235238
(() => x + y + v);
236239
if (x == 1) {
237240
~~~~~~
238-
!!! error TS2365: Operator '==' cannot be applied to types '0' and '1'.
241+
!!! error TS2367: The types of these values indicate that this condition will always be 'false'.
242+
!!! error TS2367: Operator '==' cannot be applied to types '0' and '1'.
239243
return;
240244
}
241245
}

tests/baselines/reference/capturedLetConstInLoop5_ES6.errors.txt

+8-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
tests/cases/compiler/capturedLetConstInLoop5_ES6.ts(174,13): error TS2365: Operator '==' cannot be applied to types '0' and '1'.
2-
tests/cases/compiler/capturedLetConstInLoop5_ES6.ts(229,13): error TS2365: Operator '==' cannot be applied to types '0' and '1'.
1+
tests/cases/compiler/capturedLetConstInLoop5_ES6.ts(174,13): error TS2367: The types of these values indicate that this condition will always be 'false'.
2+
Operator '==' cannot be applied to types '0' and '1'.
3+
tests/cases/compiler/capturedLetConstInLoop5_ES6.ts(229,13): error TS2367: The types of these values indicate that this condition will always be 'false'.
4+
Operator '==' cannot be applied to types '0' and '1'.
35

46

57
==== tests/cases/compiler/capturedLetConstInLoop5_ES6.ts (2 errors) ====
@@ -178,7 +180,8 @@ tests/cases/compiler/capturedLetConstInLoop5_ES6.ts(229,13): error TS2365: Opera
178180
(() => x + v);
179181
if (x == 1) {
180182
~~~~~~
181-
!!! error TS2365: Operator '==' cannot be applied to types '0' and '1'.
183+
!!! error TS2367: The types of these values indicate that this condition will always be 'false'.
184+
!!! error TS2367: Operator '==' cannot be applied to types '0' and '1'.
182185
return;
183186
}
184187
}
@@ -235,7 +238,8 @@ tests/cases/compiler/capturedLetConstInLoop5_ES6.ts(229,13): error TS2365: Opera
235238
(() => x + y + v);
236239
if (x == 1) {
237240
~~~~~~
238-
!!! error TS2365: Operator '==' cannot be applied to types '0' and '1'.
241+
!!! error TS2367: The types of these values indicate that this condition will always be 'false'.
242+
!!! error TS2367: Operator '==' cannot be applied to types '0' and '1'.
239243
return;
240244
}
241245
}

tests/baselines/reference/capturedLetConstInLoop6.errors.txt

+16-8
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1-
tests/cases/compiler/capturedLetConstInLoop6.ts(147,9): error TS2365: Operator '==' cannot be applied to types '0' and '1'.
2-
tests/cases/compiler/capturedLetConstInLoop6.ts(150,9): error TS2365: Operator '==' cannot be applied to types '0' and '2'.
3-
tests/cases/compiler/capturedLetConstInLoop6.ts(194,9): error TS2365: Operator '==' cannot be applied to types '0' and '1'.
4-
tests/cases/compiler/capturedLetConstInLoop6.ts(197,9): error TS2365: Operator '==' cannot be applied to types '0' and '2'.
1+
tests/cases/compiler/capturedLetConstInLoop6.ts(147,9): error TS2367: The types of these values indicate that this condition will always be 'false'.
2+
Operator '==' cannot be applied to types '0' and '1'.
3+
tests/cases/compiler/capturedLetConstInLoop6.ts(150,9): error TS2367: The types of these values indicate that this condition will always be 'false'.
4+
Operator '==' cannot be applied to types '0' and '2'.
5+
tests/cases/compiler/capturedLetConstInLoop6.ts(194,9): error TS2367: The types of these values indicate that this condition will always be 'false'.
6+
Operator '==' cannot be applied to types '0' and '1'.
7+
tests/cases/compiler/capturedLetConstInLoop6.ts(197,9): error TS2367: The types of these values indicate that this condition will always be 'false'.
8+
Operator '==' cannot be applied to types '0' and '2'.
59

610

711
==== tests/cases/compiler/capturedLetConstInLoop6.ts (4 errors) ====
@@ -153,12 +157,14 @@ tests/cases/compiler/capturedLetConstInLoop6.ts(197,9): error TS2365: Operator '
153157
(() => x);
154158
if (x == 1) {
155159
~~~~~~
156-
!!! error TS2365: Operator '==' cannot be applied to types '0' and '1'.
160+
!!! error TS2367: The types of these values indicate that this condition will always be 'false'.
161+
!!! error TS2367: Operator '==' cannot be applied to types '0' and '1'.
157162
break;
158163
}
159164
if (x == 2) {
160165
~~~~~~
161-
!!! error TS2365: Operator '==' cannot be applied to types '0' and '2'.
166+
!!! error TS2367: The types of these values indicate that this condition will always be 'false'.
167+
!!! error TS2367: Operator '==' cannot be applied to types '0' and '2'.
162168
continue;
163169
}
164170
}
@@ -204,12 +210,14 @@ tests/cases/compiler/capturedLetConstInLoop6.ts(197,9): error TS2365: Operator '
204210
(() => x + y);
205211
if (x == 1) {
206212
~~~~~~
207-
!!! error TS2365: Operator '==' cannot be applied to types '0' and '1'.
213+
!!! error TS2367: The types of these values indicate that this condition will always be 'false'.
214+
!!! error TS2367: Operator '==' cannot be applied to types '0' and '1'.
208215
break;
209216
}
210217
if (x == 2) {
211218
~~~~~~
212-
!!! error TS2365: Operator '==' cannot be applied to types '0' and '2'.
219+
!!! error TS2367: The types of these values indicate that this condition will always be 'false'.
220+
!!! error TS2367: Operator '==' cannot be applied to types '0' and '2'.
213221
continue;
214222
}
215223
}

tests/baselines/reference/capturedLetConstInLoop6_ES6.errors.txt

+16-8
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1-
tests/cases/compiler/capturedLetConstInLoop6_ES6.ts(147,9): error TS2365: Operator '==' cannot be applied to types '0' and '1'.
2-
tests/cases/compiler/capturedLetConstInLoop6_ES6.ts(150,9): error TS2365: Operator '==' cannot be applied to types '0' and '2'.
3-
tests/cases/compiler/capturedLetConstInLoop6_ES6.ts(194,9): error TS2365: Operator '==' cannot be applied to types '0' and '1'.
4-
tests/cases/compiler/capturedLetConstInLoop6_ES6.ts(197,9): error TS2365: Operator '==' cannot be applied to types '0' and '2'.
1+
tests/cases/compiler/capturedLetConstInLoop6_ES6.ts(147,9): error TS2367: The types of these values indicate that this condition will always be 'false'.
2+
Operator '==' cannot be applied to types '0' and '1'.
3+
tests/cases/compiler/capturedLetConstInLoop6_ES6.ts(150,9): error TS2367: The types of these values indicate that this condition will always be 'false'.
4+
Operator '==' cannot be applied to types '0' and '2'.
5+
tests/cases/compiler/capturedLetConstInLoop6_ES6.ts(194,9): error TS2367: The types of these values indicate that this condition will always be 'false'.
6+
Operator '==' cannot be applied to types '0' and '1'.
7+
tests/cases/compiler/capturedLetConstInLoop6_ES6.ts(197,9): error TS2367: The types of these values indicate that this condition will always be 'false'.
8+
Operator '==' cannot be applied to types '0' and '2'.
59

610

711
==== tests/cases/compiler/capturedLetConstInLoop6_ES6.ts (4 errors) ====
@@ -153,12 +157,14 @@ tests/cases/compiler/capturedLetConstInLoop6_ES6.ts(197,9): error TS2365: Operat
153157
(() => x);
154158
if (x == 1) {
155159
~~~~~~
156-
!!! error TS2365: Operator '==' cannot be applied to types '0' and '1'.
160+
!!! error TS2367: The types of these values indicate that this condition will always be 'false'.
161+
!!! error TS2367: Operator '==' cannot be applied to types '0' and '1'.
157162
break;
158163
}
159164
if (x == 2) {
160165
~~~~~~
161-
!!! error TS2365: Operator '==' cannot be applied to types '0' and '2'.
166+
!!! error TS2367: The types of these values indicate that this condition will always be 'false'.
167+
!!! error TS2367: Operator '==' cannot be applied to types '0' and '2'.
162168
continue;
163169
}
164170
}
@@ -204,12 +210,14 @@ tests/cases/compiler/capturedLetConstInLoop6_ES6.ts(197,9): error TS2365: Operat
204210
(() => x + y);
205211
if (x == 1) {
206212
~~~~~~
207-
!!! error TS2365: Operator '==' cannot be applied to types '0' and '1'.
213+
!!! error TS2367: The types of these values indicate that this condition will always be 'false'.
214+
!!! error TS2367: Operator '==' cannot be applied to types '0' and '1'.
208215
break;
209216
}
210217
if (x == 2) {
211218
~~~~~~
212-
!!! error TS2365: Operator '==' cannot be applied to types '0' and '2'.
219+
!!! error TS2367: The types of these values indicate that this condition will always be 'false'.
220+
!!! error TS2367: Operator '==' cannot be applied to types '0' and '2'.
213221
continue;
214222
}
215223
}

0 commit comments

Comments
 (0)