@@ -13532,6 +13532,12 @@ namespace ts {
13532
13532
}
13533
13533
13534
13534
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
+ }
13535
13541
return (target.flags & TypeFlags.Nullable) !== 0 || isTypeComparableTo(source, target);
13536
13542
}
13537
13543
@@ -13672,12 +13678,6 @@ namespace ts {
13672
13678
case SyntaxKind.ExclamationEqualsToken:
13673
13679
case SyntaxKind.EqualsEqualsEqualsToken:
13674
13680
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
- }
13681
13681
if (!isTypeEqualityComparableTo(leftType, rightType) && !isTypeEqualityComparableTo(rightType, leftType)) {
13682
13682
reportOperatorError();
13683
13683
}
@@ -16584,7 +16584,7 @@ namespace ts {
16584
16584
let firstDefaultClause: CaseOrDefaultClause;
16585
16585
let hasDuplicateDefaultClause = false;
16586
16586
16587
- let expressionType = checkExpression(node.expression);
16587
+ const expressionType = checkExpression(node.expression);
16588
16588
forEach(node.caseBlock.clauses, clause => {
16589
16589
// Grammar check for duplicate default clauses, skip if we already report duplicate default clause
16590
16590
if (clause.kind === SyntaxKind.DefaultClause && !hasDuplicateDefaultClause) {
@@ -16605,13 +16605,7 @@ namespace ts {
16605
16605
// TypeScript 1.0 spec (April 2014): 5.9
16606
16606
// In a 'switch' statement, each 'case' expression must be of a type that is comparable
16607
16607
// 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);
16615
16609
if (!isTypeEqualityComparableTo(expressionType, caseType)) {
16616
16610
// expressionType is not comparable to caseType, try the reversed check and report errors if it fails
16617
16611
checkTypeComparableTo(caseType, expressionType, caseClause.expression, /*headMessage*/ undefined);
0 commit comments