Skip to content

Improve error message when comparing non-comparable types #28023

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22834,10 +22834,9 @@ namespace ts {
switch (operatorToken.kind) {
case SyntaxKind.EqualsEqualsEqualsToken:
case SyntaxKind.EqualsEqualsToken:
return error(errNode, Diagnostics.This_condition_will_always_return_0_since_the_types_1_and_2_have_no_overlap, "false", leftStr, rightStr);
case SyntaxKind.ExclamationEqualsEqualsToken:
case SyntaxKind.ExclamationEqualsToken:
return error(errNode, Diagnostics.This_condition_will_always_return_0_since_the_types_1_and_2_have_no_overlap, "true", leftStr, rightStr);
return error(errNode, Diagnostics.Comparison_of_expressions_of_types_0_and_1_may_be_a_mistake_because_neither_type_sufficiently_overlaps_with_the_other_If_this_was_intentional_convert_one_of_the_expressions_to_unknown_first, leftStr, rightStr);
}
return undefined;
}
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -1292,7 +1292,7 @@
"category": "Error",
"code": 2366
},
"This condition will always return '{0}' since the types '{1}' and '{2}' have no overlap.": {
"Comparison of expressions of types '{0}' and '{1}' may be a mistake because neither type sufficiently overlaps with the other. If this was intentional, convert one of the expressions to 'unknown' first.": {
"category": "Error",
"code": 2367
},
Expand Down
6 changes: 3 additions & 3 deletions src/testRunner/unittests/tsserver/skipLibCheck.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ namespace ts.projectSystem {
);
const errorResult = <protocol.Diagnostic[]>session.executeCommand(getErrRequest).response;
assert.isTrue(errorResult.length === 1);
assert.equal(errorResult[0].code, Diagnostics.This_condition_will_always_return_0_since_the_types_1_and_2_have_no_overlap.code);
assert.equal(errorResult[0].code, Diagnostics.Comparison_of_expressions_of_types_0_and_1_may_be_a_mistake_because_neither_type_sufficiently_overlaps_with_the_other_If_this_was_intentional_convert_one_of_the_expressions_to_unknown_first.code);
});

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

it("should report semantic errors for configured js project with checkJs=true and skipLibCheck=true", () => {
Expand Down Expand Up @@ -223,7 +223,7 @@ namespace ts.projectSystem {
);
const errorResult = <protocol.Diagnostic[]>session.executeCommand(getErrRequest).response;
assert.isTrue(errorResult.length === 1);
assert.equal(errorResult[0].code, Diagnostics.This_condition_will_always_return_0_since_the_types_1_and_2_have_no_overlap.code);
assert.equal(errorResult[0].code, Diagnostics.Comparison_of_expressions_of_types_0_and_1_may_be_a_mistake_because_neither_type_sufficiently_overlaps_with_the_other_If_this_was_intentional_convert_one_of_the_expressions_to_unknown_first.code);
});
});
}
8 changes: 4 additions & 4 deletions tests/baselines/reference/capturedLetConstInLoop5.errors.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
tests/cases/compiler/capturedLetConstInLoop5.ts(174,13): error TS2367: This condition will always return 'false' since the types '0' and '1' have no overlap.
tests/cases/compiler/capturedLetConstInLoop5.ts(229,13): error TS2367: This condition will always return 'false' since the types '0' and '1' have no overlap.
tests/cases/compiler/capturedLetConstInLoop5.ts(174,13): error TS2367: Comparison of expressions of types '0' and '1' may be a mistake because neither type sufficiently overlaps with the other. If this was intentional, convert one of the expressions to 'unknown' first.
tests/cases/compiler/capturedLetConstInLoop5.ts(229,13): error TS2367: Comparison of expressions of types '0' and '1' may be a mistake because neither type sufficiently overlaps with the other. If this was intentional, convert one of the expressions to 'unknown' first.


==== tests/cases/compiler/capturedLetConstInLoop5.ts (2 errors) ====
Expand Down Expand Up @@ -178,7 +178,7 @@ tests/cases/compiler/capturedLetConstInLoop5.ts(229,13): error TS2367: This cond
(() => x + v);
if (x == 1) {
~~~~~~
!!! error TS2367: This condition will always return 'false' since the types '0' and '1' have no overlap.
!!! error TS2367: Comparison of expressions of types '0' and '1' may be a mistake because neither type sufficiently overlaps with the other. If this was intentional, convert one of the expressions to 'unknown' first.
return;
}
}
Expand Down Expand Up @@ -235,7 +235,7 @@ tests/cases/compiler/capturedLetConstInLoop5.ts(229,13): error TS2367: This cond
(() => x + y + v);
if (x == 1) {
~~~~~~
!!! error TS2367: This condition will always return 'false' since the types '0' and '1' have no overlap.
!!! error TS2367: Comparison of expressions of types '0' and '1' may be a mistake because neither type sufficiently overlaps with the other. If this was intentional, convert one of the expressions to 'unknown' first.
return;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
tests/cases/compiler/capturedLetConstInLoop5_ES6.ts(174,13): error TS2367: This condition will always return 'false' since the types '0' and '1' have no overlap.
tests/cases/compiler/capturedLetConstInLoop5_ES6.ts(229,13): error TS2367: This condition will always return 'false' since the types '0' and '1' have no overlap.
tests/cases/compiler/capturedLetConstInLoop5_ES6.ts(174,13): error TS2367: Comparison of expressions of types '0' and '1' may be a mistake because neither type sufficiently overlaps with the other. If this was intentional, convert one of the expressions to 'unknown' first.
tests/cases/compiler/capturedLetConstInLoop5_ES6.ts(229,13): error TS2367: Comparison of expressions of types '0' and '1' may be a mistake because neither type sufficiently overlaps with the other. If this was intentional, convert one of the expressions to 'unknown' first.


==== tests/cases/compiler/capturedLetConstInLoop5_ES6.ts (2 errors) ====
Expand Down Expand Up @@ -178,7 +178,7 @@ tests/cases/compiler/capturedLetConstInLoop5_ES6.ts(229,13): error TS2367: This
(() => x + v);
if (x == 1) {
~~~~~~
!!! error TS2367: This condition will always return 'false' since the types '0' and '1' have no overlap.
!!! error TS2367: Comparison of expressions of types '0' and '1' may be a mistake because neither type sufficiently overlaps with the other. If this was intentional, convert one of the expressions to 'unknown' first.
return;
}
}
Expand Down Expand Up @@ -235,7 +235,7 @@ tests/cases/compiler/capturedLetConstInLoop5_ES6.ts(229,13): error TS2367: This
(() => x + y + v);
if (x == 1) {
~~~~~~
!!! error TS2367: This condition will always return 'false' since the types '0' and '1' have no overlap.
!!! error TS2367: Comparison of expressions of types '0' and '1' may be a mistake because neither type sufficiently overlaps with the other. If this was intentional, convert one of the expressions to 'unknown' first.
return;
}
}
Expand Down
16 changes: 8 additions & 8 deletions tests/baselines/reference/capturedLetConstInLoop6.errors.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
tests/cases/compiler/capturedLetConstInLoop6.ts(147,9): error TS2367: This condition will always return 'false' since the types '0' and '1' have no overlap.
tests/cases/compiler/capturedLetConstInLoop6.ts(150,9): error TS2367: This condition will always return 'false' since the types '0' and '2' have no overlap.
tests/cases/compiler/capturedLetConstInLoop6.ts(194,9): error TS2367: This condition will always return 'false' since the types '0' and '1' have no overlap.
tests/cases/compiler/capturedLetConstInLoop6.ts(197,9): error TS2367: This condition will always return 'false' since the types '0' and '2' have no overlap.
tests/cases/compiler/capturedLetConstInLoop6.ts(147,9): error TS2367: Comparison of expressions of types '0' and '1' may be a mistake because neither type sufficiently overlaps with the other. If this was intentional, convert one of the expressions to 'unknown' first.
tests/cases/compiler/capturedLetConstInLoop6.ts(150,9): error TS2367: Comparison of expressions of types '0' and '2' may be a mistake because neither type sufficiently overlaps with the other. If this was intentional, convert one of the expressions to 'unknown' first.
tests/cases/compiler/capturedLetConstInLoop6.ts(194,9): error TS2367: Comparison of expressions of types '0' and '1' may be a mistake because neither type sufficiently overlaps with the other. If this was intentional, convert one of the expressions to 'unknown' first.
tests/cases/compiler/capturedLetConstInLoop6.ts(197,9): error TS2367: Comparison of expressions of types '0' and '2' may be a mistake because neither type sufficiently overlaps with the other. If this was intentional, convert one of the expressions to 'unknown' first.


==== tests/cases/compiler/capturedLetConstInLoop6.ts (4 errors) ====
Expand Down Expand Up @@ -153,12 +153,12 @@ tests/cases/compiler/capturedLetConstInLoop6.ts(197,9): error TS2367: This condi
(() => x);
if (x == 1) {
~~~~~~
!!! error TS2367: This condition will always return 'false' since the types '0' and '1' have no overlap.
!!! error TS2367: Comparison of expressions of types '0' and '1' may be a mistake because neither type sufficiently overlaps with the other. If this was intentional, convert one of the expressions to 'unknown' first.
break;
}
if (x == 2) {
~~~~~~
!!! error TS2367: This condition will always return 'false' since the types '0' and '2' have no overlap.
!!! error TS2367: Comparison of expressions of types '0' and '2' may be a mistake because neither type sufficiently overlaps with the other. If this was intentional, convert one of the expressions to 'unknown' first.
continue;
}
}
Expand Down Expand Up @@ -204,12 +204,12 @@ tests/cases/compiler/capturedLetConstInLoop6.ts(197,9): error TS2367: This condi
(() => x + y);
if (x == 1) {
~~~~~~
!!! error TS2367: This condition will always return 'false' since the types '0' and '1' have no overlap.
!!! error TS2367: Comparison of expressions of types '0' and '1' may be a mistake because neither type sufficiently overlaps with the other. If this was intentional, convert one of the expressions to 'unknown' first.
break;
}
if (x == 2) {
~~~~~~
!!! error TS2367: This condition will always return 'false' since the types '0' and '2' have no overlap.
!!! error TS2367: Comparison of expressions of types '0' and '2' may be a mistake because neither type sufficiently overlaps with the other. If this was intentional, convert one of the expressions to 'unknown' first.
continue;
}
}
Expand Down
16 changes: 8 additions & 8 deletions tests/baselines/reference/capturedLetConstInLoop6_ES6.errors.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
tests/cases/compiler/capturedLetConstInLoop6_ES6.ts(147,9): error TS2367: This condition will always return 'false' since the types '0' and '1' have no overlap.
tests/cases/compiler/capturedLetConstInLoop6_ES6.ts(150,9): error TS2367: This condition will always return 'false' since the types '0' and '2' have no overlap.
tests/cases/compiler/capturedLetConstInLoop6_ES6.ts(194,9): error TS2367: This condition will always return 'false' since the types '0' and '1' have no overlap.
tests/cases/compiler/capturedLetConstInLoop6_ES6.ts(197,9): error TS2367: This condition will always return 'false' since the types '0' and '2' have no overlap.
tests/cases/compiler/capturedLetConstInLoop6_ES6.ts(147,9): error TS2367: Comparison of expressions of types '0' and '1' may be a mistake because neither type sufficiently overlaps with the other. If this was intentional, convert one of the expressions to 'unknown' first.
tests/cases/compiler/capturedLetConstInLoop6_ES6.ts(150,9): error TS2367: Comparison of expressions of types '0' and '2' may be a mistake because neither type sufficiently overlaps with the other. If this was intentional, convert one of the expressions to 'unknown' first.
tests/cases/compiler/capturedLetConstInLoop6_ES6.ts(194,9): error TS2367: Comparison of expressions of types '0' and '1' may be a mistake because neither type sufficiently overlaps with the other. If this was intentional, convert one of the expressions to 'unknown' first.
tests/cases/compiler/capturedLetConstInLoop6_ES6.ts(197,9): error TS2367: Comparison of expressions of types '0' and '2' may be a mistake because neither type sufficiently overlaps with the other. If this was intentional, convert one of the expressions to 'unknown' first.


==== tests/cases/compiler/capturedLetConstInLoop6_ES6.ts (4 errors) ====
Expand Down Expand Up @@ -153,12 +153,12 @@ tests/cases/compiler/capturedLetConstInLoop6_ES6.ts(197,9): error TS2367: This c
(() => x);
if (x == 1) {
~~~~~~
!!! error TS2367: This condition will always return 'false' since the types '0' and '1' have no overlap.
!!! error TS2367: Comparison of expressions of types '0' and '1' may be a mistake because neither type sufficiently overlaps with the other. If this was intentional, convert one of the expressions to 'unknown' first.
break;
}
if (x == 2) {
~~~~~~
!!! error TS2367: This condition will always return 'false' since the types '0' and '2' have no overlap.
!!! error TS2367: Comparison of expressions of types '0' and '2' may be a mistake because neither type sufficiently overlaps with the other. If this was intentional, convert one of the expressions to 'unknown' first.
continue;
}
}
Expand Down Expand Up @@ -204,12 +204,12 @@ tests/cases/compiler/capturedLetConstInLoop6_ES6.ts(197,9): error TS2367: This c
(() => x + y);
if (x == 1) {
~~~~~~
!!! error TS2367: This condition will always return 'false' since the types '0' and '1' have no overlap.
!!! error TS2367: Comparison of expressions of types '0' and '1' may be a mistake because neither type sufficiently overlaps with the other. If this was intentional, convert one of the expressions to 'unknown' first.
break;
}
if (x == 2) {
~~~~~~
!!! error TS2367: This condition will always return 'false' since the types '0' and '2' have no overlap.
!!! error TS2367: Comparison of expressions of types '0' and '2' may be a mistake because neither type sufficiently overlaps with the other. If this was intentional, convert one of the expressions to 'unknown' first.
continue;
}
}
Expand Down
Loading