Skip to content

Commit 67ca82b

Browse files
Add flag to omit default case (#33574)
1 parent 5e0f584 commit 67ca82b

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
@@ -19244,7 +19244,9 @@ namespace ts {
1924419244

1924519245
// Get the types from all cases in a switch on `typeof`. An
1924619246
// `undefined` element denotes an explicit `default` clause.
19247-
function getSwitchClauseTypeOfWitnesses(switchStatement: SwitchStatement): (string | undefined)[] {
19247+
function getSwitchClauseTypeOfWitnesses(switchStatement: SwitchStatement, retainDefault: false): string[];
19248+
function getSwitchClauseTypeOfWitnesses(switchStatement: SwitchStatement, retainDefault: boolean): (string | undefined)[];
19249+
function getSwitchClauseTypeOfWitnesses(switchStatement: SwitchStatement, retainDefault: boolean): (string | undefined)[] {
1924819250
const witnesses: (string | undefined)[] = [];
1924919251
for (const clause of switchStatement.caseBlock.clauses) {
1925019252
if (clause.kind === SyntaxKind.CaseClause) {
@@ -19254,7 +19256,7 @@ namespace ts {
1925419256
}
1925519257
return emptyArray;
1925619258
}
19257-
witnesses.push(/*explicitDefaultStatement*/ undefined);
19259+
if (retainDefault) witnesses.push(/*explicitDefaultStatement*/ undefined);
1925819260
}
1925919261
return witnesses;
1926019262
}
@@ -20359,7 +20361,7 @@ namespace ts {
2035920361
}
2036020362

2036120363
function narrowBySwitchOnTypeOf(type: Type, switchStatement: SwitchStatement, clauseStart: number, clauseEnd: number): Type {
20362-
const switchWitnesses = getSwitchClauseTypeOfWitnesses(switchStatement);
20364+
const switchWitnesses = getSwitchClauseTypeOfWitnesses(switchStatement, /*retainDefault*/ true);
2036320365
if (!switchWitnesses.length) {
2036420366
return type;
2036520367
}
@@ -26772,8 +26774,7 @@ namespace ts {
2677226774
function computeExhaustiveSwitchStatement(node: SwitchStatement): boolean {
2677326775
if (node.expression.kind === SyntaxKind.TypeOfExpression) {
2677426776
const operandType = getTypeOfExpression((node.expression as TypeOfExpression).expression);
26775-
// This cast is safe because the switch is possibly exhaustive and does not contain a default case, so there can be no undefined.
26776-
const witnesses = <string[]>getSwitchClauseTypeOfWitnesses(node);
26777+
const witnesses = getSwitchClauseTypeOfWitnesses(node, /*retainDefault*/ false);
2677726778
// notEqualFacts states that the type of the switched value is not equal to every type in the switch.
2677826779
const notEqualFacts = getFactsFromTypeofSwitch(0, 0, witnesses, /*hasDefault*/ true);
2677926780
const type = getBaseConstraintOfType(operandType) || operandType;

0 commit comments

Comments
 (0)