mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-05-30 01:04:49 -05:00
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:
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user