diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 7f56a22bdc5..f4ec3e8bf66 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -19244,7 +19244,9 @@ namespace ts { // Get the types from all cases in a switch on `typeof`. An // `undefined` element denotes an explicit `default` clause. - function getSwitchClauseTypeOfWitnesses(switchStatement: SwitchStatement): (string | undefined)[] { + function getSwitchClauseTypeOfWitnesses(switchStatement: SwitchStatement, retainDefault: false): string[]; + function getSwitchClauseTypeOfWitnesses(switchStatement: SwitchStatement, retainDefault: boolean): (string | undefined)[]; + function getSwitchClauseTypeOfWitnesses(switchStatement: SwitchStatement, retainDefault: boolean): (string | undefined)[] { const witnesses: (string | undefined)[] = []; for (const clause of switchStatement.caseBlock.clauses) { if (clause.kind === SyntaxKind.CaseClause) { @@ -19254,7 +19256,7 @@ namespace ts { } return emptyArray; } - witnesses.push(/*explicitDefaultStatement*/ undefined); + if (retainDefault) witnesses.push(/*explicitDefaultStatement*/ undefined); } return witnesses; } @@ -20359,7 +20361,7 @@ namespace ts { } function narrowBySwitchOnTypeOf(type: Type, switchStatement: SwitchStatement, clauseStart: number, clauseEnd: number): Type { - const switchWitnesses = getSwitchClauseTypeOfWitnesses(switchStatement); + const switchWitnesses = getSwitchClauseTypeOfWitnesses(switchStatement, /*retainDefault*/ true); if (!switchWitnesses.length) { return type; } @@ -26772,8 +26774,7 @@ namespace ts { function computeExhaustiveSwitchStatement(node: SwitchStatement): boolean { if (node.expression.kind === SyntaxKind.TypeOfExpression) { const operandType = getTypeOfExpression((node.expression as TypeOfExpression).expression); - // This cast is safe because the switch is possibly exhaustive and does not contain a default case, so there can be no undefined. - const witnesses = getSwitchClauseTypeOfWitnesses(node); + const witnesses = getSwitchClauseTypeOfWitnesses(node, /*retainDefault*/ false); // notEqualFacts states that the type of the switched value is not equal to every type in the switch. const notEqualFacts = getFactsFromTypeofSwitch(0, 0, witnesses, /*hasDefault*/ true); const type = getBaseConstraintOfType(operandType) || operandType;