@@ -16908,7 +16908,9 @@ namespace ts {
16908
16908
16909
16909
// Get the types from all cases in a switch on `typeof`. An
16910
16910
// `undefined` element denotes an explicit `default` clause.
16911
- function getSwitchClauseTypeOfWitnesses(switchStatement: SwitchStatement): (string | undefined)[] {
16911
+ function getSwitchClauseTypeOfWitnesses(switchStatement: SwitchStatement, retainDefault: false): string[];
16912
+ function getSwitchClauseTypeOfWitnesses(switchStatement: SwitchStatement, retainDefault: boolean): (string | undefined)[];
16913
+ function getSwitchClauseTypeOfWitnesses(switchStatement: SwitchStatement, retainDefault: boolean): (string | undefined)[] {
16912
16914
const witnesses: (string | undefined)[] = [];
16913
16915
for (const clause of switchStatement.caseBlock.clauses) {
16914
16916
if (clause.kind === SyntaxKind.CaseClause) {
@@ -16918,7 +16920,7 @@ namespace ts {
16918
16920
}
16919
16921
return emptyArray;
16920
16922
}
16921
- witnesses.push(/*explicitDefaultStatement*/ undefined);
16923
+ if (retainDefault) witnesses.push(/*explicitDefaultStatement*/ undefined);
16922
16924
}
16923
16925
return witnesses;
16924
16926
}
@@ -17934,7 +17936,7 @@ namespace ts {
17934
17936
}
17935
17937
17936
17938
function narrowBySwitchOnTypeOf(type: Type, switchStatement: SwitchStatement, clauseStart: number, clauseEnd: number): Type {
17937
- const switchWitnesses = getSwitchClauseTypeOfWitnesses(switchStatement);
17939
+ const switchWitnesses = getSwitchClauseTypeOfWitnesses(switchStatement, /*retainDefault*/ true );
17938
17940
if (!switchWitnesses.length) {
17939
17941
return type;
17940
17942
}
@@ -24014,8 +24016,7 @@ namespace ts {
24014
24016
function computeExhaustiveSwitchStatement(node: SwitchStatement): boolean {
24015
24017
if (node.expression.kind === SyntaxKind.TypeOfExpression) {
24016
24018
const operandType = getTypeOfExpression((node.expression as TypeOfExpression).expression);
24017
- // This cast is safe because the switch is possibly exhaustive and does not contain a default case, so there can be no undefined.
24018
- const witnesses = <string[]>getSwitchClauseTypeOfWitnesses(node);
24019
+ const witnesses = getSwitchClauseTypeOfWitnesses(node, /*retainDefault*/ false);
24019
24020
// notEqualFacts states that the type of the switched value is not equal to every type in the switch.
24020
24021
const notEqualFacts = getFactsFromTypeofSwitch(0, 0, witnesses, /*hasDefault*/ true);
24021
24022
const type = getBaseConstraintOfType(operandType) || operandType;
0 commit comments