Add flag to omit default case (#33574)

This commit is contained in:
Jack Williams 2020-03-10 22:04:03 +00:00 committed by GitHub
parent 5e0f584b67
commit 67ca82b375
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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 = <string[]>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;