Skip to content

Commit df61cca

Browse files
committed
Add flag to omit default case
1 parent bf992a5 commit df61cca

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
@@ -16908,7 +16908,9 @@ namespace ts {
1690816908

1690916909
// Get the types from all cases in a switch on `typeof`. An
1691016910
// `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)[] {
1691216914
const witnesses: (string | undefined)[] = [];
1691316915
for (const clause of switchStatement.caseBlock.clauses) {
1691416916
if (clause.kind === SyntaxKind.CaseClause) {
@@ -16918,7 +16920,7 @@ namespace ts {
1691816920
}
1691916921
return emptyArray;
1692016922
}
16921-
witnesses.push(/*explicitDefaultStatement*/ undefined);
16923+
if (retainDefault) witnesses.push(/*explicitDefaultStatement*/ undefined);
1692216924
}
1692316925
return witnesses;
1692416926
}
@@ -17934,7 +17936,7 @@ namespace ts {
1793417936
}
1793517937

1793617938
function narrowBySwitchOnTypeOf(type: Type, switchStatement: SwitchStatement, clauseStart: number, clauseEnd: number): Type {
17937-
const switchWitnesses = getSwitchClauseTypeOfWitnesses(switchStatement);
17939+
const switchWitnesses = getSwitchClauseTypeOfWitnesses(switchStatement, /*retainDefault*/ true);
1793817940
if (!switchWitnesses.length) {
1793917941
return type;
1794017942
}
@@ -24014,8 +24016,7 @@ namespace ts {
2401424016
function computeExhaustiveSwitchStatement(node: SwitchStatement): boolean {
2401524017
if (node.expression.kind === SyntaxKind.TypeOfExpression) {
2401624018
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);
2401924020
// notEqualFacts states that the type of the switched value is not equal to every type in the switch.
2402024021
const notEqualFacts = getFactsFromTypeofSwitch(0, 0, witnesses, /*hasDefault*/ true);
2402124022
const type = getBaseConstraintOfType(operandType) || operandType;

0 commit comments

Comments
 (0)