Skip to content

Commit 0168f67

Browse files
committed
Push eitherIsNotLiteral check into isTypeEqualityComparableTo
Since all callers need this check -- it's how equality is supposed to work everywhere.
1 parent 982abc2 commit 0168f67

File tree

1 file changed

+8
-14
lines changed

1 file changed

+8
-14
lines changed

src/compiler/checker.ts

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13532,6 +13532,12 @@ namespace ts {
1353213532
}
1353313533

1353413534
function isTypeEqualityComparableTo(source: Type, target: Type) {
13535+
const sourceIsLiteral = isLiteralType(source);
13536+
const targetIsLiteral = isLiteralType(target);
13537+
if (!sourceIsLiteral || !targetIsLiteral) {
13538+
source = sourceIsLiteral ? getBaseTypeOfLiteralType(source) : source;
13539+
target = targetIsLiteral ? getBaseTypeOfLiteralType(target) : target;
13540+
}
1353513541
return (target.flags & TypeFlags.Nullable) !== 0 || isTypeComparableTo(source, target);
1353613542
}
1353713543

@@ -13672,12 +13678,6 @@ namespace ts {
1367213678
case SyntaxKind.ExclamationEqualsToken:
1367313679
case SyntaxKind.EqualsEqualsEqualsToken:
1367413680
case SyntaxKind.ExclamationEqualsEqualsToken:
13675-
const leftIsLiteral = isLiteralType(leftType);
13676-
const rightIsLiteral = isLiteralType(rightType);
13677-
if (!leftIsLiteral || !rightIsLiteral) {
13678-
leftType = leftIsLiteral ? getBaseTypeOfLiteralType(leftType) : leftType;
13679-
rightType = rightIsLiteral ? getBaseTypeOfLiteralType(rightType) : rightType;
13680-
}
1368113681
if (!isTypeEqualityComparableTo(leftType, rightType) && !isTypeEqualityComparableTo(rightType, leftType)) {
1368213682
reportOperatorError();
1368313683
}
@@ -16584,7 +16584,7 @@ namespace ts {
1658416584
let firstDefaultClause: CaseOrDefaultClause;
1658516585
let hasDuplicateDefaultClause = false;
1658616586

16587-
let expressionType = checkExpression(node.expression);
16587+
const expressionType = checkExpression(node.expression);
1658816588
forEach(node.caseBlock.clauses, clause => {
1658916589
// Grammar check for duplicate default clauses, skip if we already report duplicate default clause
1659016590
if (clause.kind === SyntaxKind.DefaultClause && !hasDuplicateDefaultClause) {
@@ -16605,13 +16605,7 @@ namespace ts {
1660516605
// TypeScript 1.0 spec (April 2014): 5.9
1660616606
// In a 'switch' statement, each 'case' expression must be of a type that is comparable
1660716607
// to or from the type of the 'switch' expression.
16608-
let caseType = checkExpression(caseClause.expression);
16609-
const expressionIsLiteral = isLiteralType(expressionType);
16610-
const caseIsLiteral = isLiteralType(caseType);
16611-
if (!expressionIsLiteral || !caseIsLiteral) {
16612-
expressionType = expressionIsLiteral ? getBaseTypeOfLiteralType(expressionType) : expressionType;
16613-
caseType = caseIsLiteral ? getBaseTypeOfLiteralType(caseType) : caseType;
16614-
}
16608+
const caseType = checkExpression(caseClause.expression);
1661516609
if (!isTypeEqualityComparableTo(expressionType, caseType)) {
1661616610
// expressionType is not comparable to caseType, try the reversed check and report errors if it fails
1661716611
checkTypeComparableTo(caseType, expressionType, caseClause.expression, /*headMessage*/ undefined);

0 commit comments

Comments
 (0)