Skip to content
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

Improve error message for == of two different types #27227

Closed
wants to merge 4 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
63 changes: 52 additions & 11 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22449,7 +22449,20 @@ namespace ts {
const leftStr = typeToString(leftType);
const rightStr = typeToString(rightType);
const errNode = errorNode || operatorToken;
if (!tryGiveBetterPrimaryError(errNode, leftStr, rightStr)) {
const equalsInfo = getEqualsInfo(operatorToken.kind);
if (equalsInfo !== undefined) {
const explicitConversion = getExplicitConversion(leftType, rightType);
if (explicitConversion) {
const diag = explicitConversion.side === "left"
? Diagnostics.Types_0_and_1_have_no_overlap_Consider_explicitly_converting_the_left_side_using_2_x
: Diagnostics.Types_0_and_1_have_no_overlap_Consider_explicitly_converting_the_right_side_using_2_x;
error(errNode, diag, leftStr, rightStr, explicitConversion.conversionFunctionName);
}
else {
error(errNode, Diagnostics.This_condition_will_always_return_0_since_the_types_1_and_2_have_no_overlap, String(!equalsInfo), leftStr, rightStr);
}
}
else {
error(
errNode,
Diagnostics.Operator_0_cannot_be_applied_to_types_1_and_2,
Expand All @@ -22459,20 +22472,48 @@ namespace ts {
);
}
}
}

function tryGiveBetterPrimaryError(errNode: Node, leftStr: string, rightStr: string) {
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 undefined;
function getEqualsInfo(operatorToken: SyntaxKind): boolean | undefined {
switch (operatorToken) {
case SyntaxKind.EqualsEqualsEqualsToken:
case SyntaxKind.EqualsEqualsToken:
return true;
case SyntaxKind.ExclamationEqualsEqualsToken:
case SyntaxKind.ExclamationEqualsToken:
return false;
default:
return undefined;
}
}

interface ExplicitConversion {
readonly side: "left" | "right";
readonly conversionFunctionName: string;
}

function getExplicitConversion(left: Type, right: Type): ExplicitConversion | undefined {
const convertLeft = getFunctionNameToConvertToType(right, left);
if (convertLeft) return { side: "left", conversionFunctionName: convertLeft };
const convertRight = getFunctionNameToConvertToType(left, right);
if (convertRight) return { side: "right", conversionFunctionName: convertRight };
return undefined;
}

function getFunctionNameToConvertToType(to: Type, from: Type): string | undefined {
return typeOrTypesHaveFlags(to, TypeFlags.StringLike) && !typeOrTypesHaveFlags(from, TypeFlags.StringLike)
? "String"
: typeOrTypesHaveFlags(to, TypeFlags.NumberLike) && !typeOrTypesHaveFlags(from, TypeFlags.NumberLike)
? "Number"
: typeOrTypesHaveFlags(to, TypeFlags.BooleanLike) && !typeOrTypesHaveFlags(from, TypeFlags.BooleanLike)
? "Boolean"
: undefined;
}

function typeOrTypesHaveFlags(type: Type, flags: TypeFlags): boolean {
return (type.flags & TypeFlags.Union ? (type as UnionType).types : [type]).every(t => !!(t.flags & flags));
}

function isYieldExpressionInClass(node: YieldExpression): boolean {
let current: Node = node;
let parent = node.parent;
Expand Down
8 changes: 8 additions & 0 deletions src/compiler/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -2128,6 +2128,14 @@
"category": "Error",
"code": 2588
},
"Types '{0}' and '{1}' have no overlap. Consider explicitly converting the left side using `{2}(x)`.": {
"category": "Error",
"code": 2589
},
"Types '{0}' and '{1}' have no overlap. Consider explicitly converting the right side using `{2}(x)`.": {
"category": "Error",
"code": 2590
},
"JSX element attributes type '{0}' may not be a union type.": {
"category": "Error",
"code": 2600
Expand Down
6 changes: 3 additions & 3 deletions src/testRunner/unittests/tsserverProjectSystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5523,7 +5523,7 @@ var x = 10;`
);
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.Types_0_and_1_have_no_overlap_Consider_explicitly_converting_the_left_side_using_2_x.code);
});

it("should report semantic errors for configured js project with '// @ts-check' and skipLibCheck=true", () => {
Expand All @@ -5550,7 +5550,7 @@ var x = 10;`
);
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.Types_0_and_1_have_no_overlap_Consider_explicitly_converting_the_left_side_using_2_x.code);
});

it("should report semantic errors for configured js project with checkJs=true and skipLibCheck=true", () => {
Expand Down Expand Up @@ -5579,7 +5579,7 @@ var x = 10;`
);
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.Types_0_and_1_have_no_overlap_Consider_explicitly_converting_the_left_side_using_2_x.code);
});
});

Expand Down
Loading