Relax switch-case narrowing restrictions (#23522)

* Allow switch case narrowing even when individual clauses are nonunit

* And remove unit type restriction

* Rename
This commit is contained in:
Wesley Wigham
2018-04-27 16:30:43 -07:00
committed by GitHub
parent 47385b29e0
commit 8c5ad2429a
7 changed files with 280 additions and 14 deletions

View File

@@ -11570,6 +11570,10 @@ namespace ts {
return !!getPropertyOfType(type, "0" as __String);
}
function isNeitherUnitTypeNorNever(type: Type): boolean {
return !(type.flags & (TypeFlags.Unit | TypeFlags.Never));
}
function isUnitType(type: Type): boolean {
return !!(type.flags & TypeFlags.Unit);
}
@@ -13049,8 +13053,7 @@ namespace ts {
function getTypeOfSwitchClause(clause: CaseClause | DefaultClause) {
if (clause.kind === SyntaxKind.CaseClause) {
const caseType = getRegularTypeOfLiteralType(getTypeOfExpression(clause.expression));
return isUnitType(caseType) ? caseType : undefined;
return getRegularTypeOfLiteralType(getTypeOfExpression(clause.expression));
}
return neverType;
}
@@ -13058,15 +13061,9 @@ namespace ts {
function getSwitchClauseTypes(switchStatement: SwitchStatement): Type[] {
const links = getNodeLinks(switchStatement);
if (!links.switchTypes) {
// If all case clauses specify expressions that have unit types, we return an array
// of those unit types. Otherwise we return an empty array.
links.switchTypes = [];
for (const clause of switchStatement.caseBlock.clauses) {
const type = getTypeOfSwitchClause(clause);
if (type === undefined) {
return links.switchTypes = emptyArray;
}
links.switchTypes.push(type);
links.switchTypes.push(getTypeOfSwitchClause(clause));
}
}
return links.switchTypes;
@@ -19170,7 +19167,7 @@ namespace ts {
return false;
}
const switchTypes = getSwitchClauseTypes(node);
if (!switchTypes.length) {
if (!switchTypes.length || some(switchTypes, isNeitherUnitTypeNorNever)) {
return false;
}
return eachTypeContainedIn(mapType(type, getRegularTypeOfLiteralType), switchTypes);