Skip to content

Commit ccad31b

Browse files
committed
Equality comparisons for null/undefined in strict null checking mode
1 parent 7f82beb commit ccad31b

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

src/compiler/checker.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12053,6 +12053,10 @@ namespace ts {
1205312053
return sourceType;
1205412054
}
1205512055

12056+
function isTypeEqualityComparableTo(source: Type, target: Type) {
12057+
return (target.flags & TypeFlags.Nullable) !== 0 || isTypeComparableTo(source, target);
12058+
}
12059+
1205612060
function checkBinaryExpression(node: BinaryExpression, contextualMapper?: TypeMapper) {
1205712061
return checkBinaryLikeExpression(node.left, node.operatorToken, node.right, contextualMapper, node);
1205812062
}
@@ -12166,15 +12170,17 @@ namespace ts {
1216612170
case SyntaxKind.GreaterThanToken:
1216712171
case SyntaxKind.LessThanEqualsToken:
1216812172
case SyntaxKind.GreaterThanEqualsToken:
12169-
if (!checkForDisallowedESSymbolOperand(operator)) {
12170-
return booleanType;
12173+
if (checkForDisallowedESSymbolOperand(operator)) {
12174+
if (!isTypeComparableTo(leftType, rightType) && !isTypeComparableTo(rightType, leftType)) {
12175+
reportOperatorError();
12176+
}
1217112177
}
12172-
// Fall through
12178+
return booleanType;
1217312179
case SyntaxKind.EqualsEqualsToken:
1217412180
case SyntaxKind.ExclamationEqualsToken:
1217512181
case SyntaxKind.EqualsEqualsEqualsToken:
1217612182
case SyntaxKind.ExclamationEqualsEqualsToken:
12177-
if (!isTypeComparableTo(leftType, rightType) && !isTypeComparableTo(rightType, leftType)) {
12183+
if (!isTypeEqualityComparableTo(leftType, rightType) && !isTypeEqualityComparableTo(rightType, leftType)) {
1217812184
reportOperatorError();
1217912185
}
1218012186
return booleanType;

0 commit comments

Comments
 (0)