Skip to content

Commit d7f5fc8

Browse files
committed
Get literal type only once
1 parent ebef408 commit d7f5fc8

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

src/compiler/checker.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16882,7 +16882,8 @@ namespace ts {
1688216882
let firstDefaultClause: CaseOrDefaultClause;
1688316883
let hasDuplicateDefaultClause = false;
1688416884

16885-
let expressionType = checkExpression(node.expression);
16885+
const expressionType = checkExpression(node.expression);
16886+
const expressionIsLiteral = isLiteralType(expressionType);
1688616887
forEach(node.caseBlock.clauses, clause => {
1688716888
// Grammar check for duplicate default clauses, skip if we already report duplicate default clause
1688816889
if (clause.kind === SyntaxKind.DefaultClause && !hasDuplicateDefaultClause) {
@@ -16905,14 +16906,14 @@ namespace ts {
1690516906
// to or from the type of the 'switch' expression.
1690616907
let caseType = checkExpression(caseClause.expression);
1690716908
const caseIsLiteral = isLiteralType(caseType);
16908-
const expressionIsLiteral = isLiteralType(expressionType);
16909+
let literalExpressionType = expressionType;
1690916910
if (!caseIsLiteral || !expressionIsLiteral) {
1691016911
caseType = caseIsLiteral ? getBaseTypeOfLiteralType(caseType) : caseType;
16911-
expressionType = expressionIsLiteral ? getBaseTypeOfLiteralType(expressionType) : expressionType;
16912+
literalExpressionType = getBaseTypeOfLiteralType(expressionType);
1691216913
}
16913-
if (!isTypeEqualityComparableTo(expressionType, caseType)) {
16914+
if (!isTypeEqualityComparableTo(literalExpressionType, caseType)) {
1691416915
// expressionType is not comparable to caseType, try the reversed check and report errors if it fails
16915-
checkTypeComparableTo(caseType, expressionType, caseClause.expression, /*headMessage*/ undefined);
16916+
checkTypeComparableTo(caseType, literalExpressionType, caseClause.expression, /*headMessage*/ undefined);
1691616917
}
1691716918
}
1691816919
forEach(clause.statements, checkSourceElement);

0 commit comments

Comments
 (0)