Skip to content

Rephrase error message when comparing unrelated types #50471

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

Merged
merged 2 commits into from
Aug 26, 2022
Merged
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
21 changes: 7 additions & 14 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34582,26 +34582,19 @@ namespace ts {
}

function tryGiveBetterPrimaryError(errNode: Node, maybeMissingAwait: boolean, leftStr: string, rightStr: string) {
let typeName: string | undefined;
switch (operatorToken.kind) {
case SyntaxKind.EqualsEqualsEqualsToken:
case SyntaxKind.EqualsEqualsToken:
typeName = "false";
break;
case SyntaxKind.ExclamationEqualsEqualsToken:
case SyntaxKind.ExclamationEqualsToken:
typeName = "true";
}

if (typeName) {
return errorAndMaybeSuggestAwait(
errNode,
maybeMissingAwait,
Diagnostics.This_condition_will_always_return_0_since_the_types_1_and_2_have_no_overlap,
typeName, leftStr, rightStr);
return errorAndMaybeSuggestAwait(
errNode,
maybeMissingAwait,
Diagnostics.This_comparison_appears_to_be_unintentional_because_the_types_0_and_1_have_no_overlap,
leftStr, rightStr);
default:
return undefined;
}

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 @@ -1864,7 +1864,7 @@
"category": "Error",
"code": 2366
},
"This condition will always return '{0}' since the types '{1}' and '{2}' have no overlap.": {
"This comparison appears to be unintentional because the types '{0}' and '{1}' have no overlap.": {
"category": "Error",
"code": 2367
},
Expand Down
2 changes: 1 addition & 1 deletion src/services/codefixes/addMissingAwait.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace ts.codefix {
Diagnostics.The_right_hand_side_of_an_arithmetic_operation_must_be_of_type_any_number_bigint_or_an_enum_type.code,
Diagnostics.Operator_0_cannot_be_applied_to_type_1.code,
Diagnostics.Operator_0_cannot_be_applied_to_types_1_and_2.code,
Diagnostics.This_condition_will_always_return_0_since_the_types_1_and_2_have_no_overlap.code,
Diagnostics.This_comparison_appears_to_be_unintentional_because_the_types_0_and_1_have_no_overlap.code,
Diagnostics.This_condition_will_always_return_true_since_this_0_is_always_defined.code,
Diagnostics.Type_0_is_not_an_array_type.code,
Diagnostics.Type_0_is_not_an_array_type_or_a_string_type.code,
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 = session.executeCommand(getErrRequest).response as protocol.Diagnostic[];
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.This_comparison_appears_to_be_unintentional_because_the_types_0_and_1_have_no_overlap.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 = session.executeCommand(getErrRequest).response as protocol.Diagnostic[];
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.This_comparison_appears_to_be_unintentional_because_the_types_0_and_1_have_no_overlap.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 = session.executeCommand(getErrRequest).response as protocol.Diagnostic[];
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.This_comparison_appears_to_be_unintentional_because_the_types_0_and_1_have_no_overlap.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: This comparison appears to be unintentional because the types '0' and '1' have no overlap.
tests/cases/compiler/capturedLetConstInLoop5.ts(229,13): error TS2367: This comparison appears to be unintentional because the types '0' and '1' have no overlap.


==== 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: This comparison appears to be unintentional because the types '0' and '1' have no overlap.
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: This comparison appears to be unintentional because the types '0' and '1' have no overlap.
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: This comparison appears to be unintentional because the types '0' and '1' have no overlap.
tests/cases/compiler/capturedLetConstInLoop5_ES6.ts(229,13): error TS2367: This comparison appears to be unintentional because the types '0' and '1' have no overlap.


==== 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: This comparison appears to be unintentional because the types '0' and '1' have no overlap.
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: This comparison appears to be unintentional because the types '0' and '1' have no overlap.
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: This comparison appears to be unintentional because the types '0' and '1' have no overlap.
tests/cases/compiler/capturedLetConstInLoop6.ts(150,9): error TS2367: This comparison appears to be unintentional because the types '0' and '2' have no overlap.
tests/cases/compiler/capturedLetConstInLoop6.ts(194,9): error TS2367: This comparison appears to be unintentional because the types '0' and '1' have no overlap.
tests/cases/compiler/capturedLetConstInLoop6.ts(197,9): error TS2367: This comparison appears to be unintentional because the types '0' and '2' have no overlap.


==== 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: This comparison appears to be unintentional because the types '0' and '1' have no overlap.
break;
}
if (x == 2) {
~~~~~~
!!! error TS2367: This condition will always return 'false' since the types '0' and '2' have no overlap.
!!! error TS2367: This comparison appears to be unintentional because the types '0' and '2' have no overlap.
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: This comparison appears to be unintentional because the types '0' and '1' have no overlap.
break;
}
if (x == 2) {
~~~~~~
!!! error TS2367: This condition will always return 'false' since the types '0' and '2' have no overlap.
!!! error TS2367: This comparison appears to be unintentional because the types '0' and '2' have no overlap.
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: This comparison appears to be unintentional because the types '0' and '1' have no overlap.
tests/cases/compiler/capturedLetConstInLoop6_ES6.ts(150,9): error TS2367: This comparison appears to be unintentional because the types '0' and '2' have no overlap.
tests/cases/compiler/capturedLetConstInLoop6_ES6.ts(194,9): error TS2367: This comparison appears to be unintentional because the types '0' and '1' have no overlap.
tests/cases/compiler/capturedLetConstInLoop6_ES6.ts(197,9): error TS2367: This comparison appears to be unintentional because the types '0' and '2' have no overlap.


==== 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: This comparison appears to be unintentional because the types '0' and '1' have no overlap.
break;
}
if (x == 2) {
~~~~~~
!!! error TS2367: This condition will always return 'false' since the types '0' and '2' have no overlap.
!!! error TS2367: This comparison appears to be unintentional because the types '0' and '2' have no overlap.
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: This comparison appears to be unintentional because the types '0' and '1' have no overlap.
break;
}
if (x == 2) {
~~~~~~
!!! error TS2367: This condition will always return 'false' since the types '0' and '2' have no overlap.
!!! error TS2367: This comparison appears to be unintentional because the types '0' and '2' have no overlap.
continue;
}
}
Expand Down
32 changes: 16 additions & 16 deletions tests/baselines/reference/capturedLetConstInLoop7.errors.txt
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
tests/cases/compiler/capturedLetConstInLoop7.ts(230,9): error TS2367: This condition will always return 'false' since the types '0' and '1' have no overlap.
tests/cases/compiler/capturedLetConstInLoop7.ts(233,9): error TS2367: This condition will always return 'false' since the types '0' and '1' have no overlap.
tests/cases/compiler/capturedLetConstInLoop7.ts(236,9): error TS2367: This condition will always return 'false' since the types '0' and '2' have no overlap.
tests/cases/compiler/capturedLetConstInLoop7.ts(239,9): error TS2367: This condition will always return 'false' since the types '0' and '2' have no overlap.
tests/cases/compiler/capturedLetConstInLoop7.ts(305,9): error TS2367: This condition will always return 'false' since the types '0' and '1' have no overlap.
tests/cases/compiler/capturedLetConstInLoop7.ts(308,9): error TS2367: This condition will always return 'false' since the types '0' and '1' have no overlap.
tests/cases/compiler/capturedLetConstInLoop7.ts(311,9): error TS2367: This condition will always return 'false' since the types '0' and '2' have no overlap.
tests/cases/compiler/capturedLetConstInLoop7.ts(314,9): error TS2367: This condition will always return 'false' since the types '0' and '2' have no overlap.
tests/cases/compiler/capturedLetConstInLoop7.ts(230,9): error TS2367: This comparison appears to be unintentional because the types '0' and '1' have no overlap.
tests/cases/compiler/capturedLetConstInLoop7.ts(233,9): error TS2367: This comparison appears to be unintentional because the types '0' and '1' have no overlap.
tests/cases/compiler/capturedLetConstInLoop7.ts(236,9): error TS2367: This comparison appears to be unintentional because the types '0' and '2' have no overlap.
tests/cases/compiler/capturedLetConstInLoop7.ts(239,9): error TS2367: This comparison appears to be unintentional because the types '0' and '2' have no overlap.
tests/cases/compiler/capturedLetConstInLoop7.ts(305,9): error TS2367: This comparison appears to be unintentional because the types '0' and '1' have no overlap.
tests/cases/compiler/capturedLetConstInLoop7.ts(308,9): error TS2367: This comparison appears to be unintentional because the types '0' and '1' have no overlap.
tests/cases/compiler/capturedLetConstInLoop7.ts(311,9): error TS2367: This comparison appears to be unintentional because the types '0' and '2' have no overlap.
tests/cases/compiler/capturedLetConstInLoop7.ts(314,9): error TS2367: This comparison appears to be unintentional because the types '0' and '2' have no overlap.


==== tests/cases/compiler/capturedLetConstInLoop7.ts (8 errors) ====
Expand Down Expand Up @@ -240,22 +240,22 @@ tests/cases/compiler/capturedLetConstInLoop7.ts(314,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: This comparison appears to be unintentional because the types '0' and '1' have no overlap.
break;
}
if (x == 1) {
~~~~~~
!!! error TS2367: This condition will always return 'false' since the types '0' and '1' have no overlap.
!!! error TS2367: This comparison appears to be unintentional because the types '0' and '1' have no overlap.
break l1_c;
}
if (x == 2) {
~~~~~~
!!! error TS2367: This condition will always return 'false' since the types '0' and '2' have no overlap.
!!! error TS2367: This comparison appears to be unintentional because the types '0' and '2' have no overlap.
continue;
}
if (x == 2) {
~~~~~~
!!! error TS2367: This condition will always return 'false' since the types '0' and '2' have no overlap.
!!! error TS2367: This comparison appears to be unintentional because the types '0' and '2' have no overlap.
continue l1_c;
}
}
Expand Down Expand Up @@ -323,22 +323,22 @@ tests/cases/compiler/capturedLetConstInLoop7.ts(314,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: This comparison appears to be unintentional because the types '0' and '1' have no overlap.
break;
}
if (x == 1) {
~~~~~~
!!! error TS2367: This condition will always return 'false' since the types '0' and '1' have no overlap.
!!! error TS2367: This comparison appears to be unintentional because the types '0' and '1' have no overlap.
break l5_c;
}
if (x == 2) {
~~~~~~
!!! error TS2367: This condition will always return 'false' since the types '0' and '2' have no overlap.
!!! error TS2367: This comparison appears to be unintentional because the types '0' and '2' have no overlap.
continue;
}
if (x == 2) {
~~~~~~
!!! error TS2367: This condition will always return 'false' since the types '0' and '2' have no overlap.
!!! error TS2367: This comparison appears to be unintentional because the types '0' and '2' have no overlap.
continue l5_c;
}
}
Expand Down
Loading