Fix EvolvingArray crash in discriminant property narrowing

Co-authored-by: RyanCavanaugh <6685088+RyanCavanaugh@users.noreply.github.com>
This commit is contained in:
copilot-swe-agent[bot]
2025-07-23 21:50:39 +00:00
parent 7f0a0e542b
commit f820fc887f

View File

@@ -29397,20 +29397,23 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
}
}
// Fix for #23572: Allow discriminant property narrowing for non-union types
// This enables narrowing to never when all possibilities are eliminated
else {
const access = getCandidateDiscriminantPropertyAccess(expr);
if (access) {
const name = getAccessedPropertyName(access);
if (name) {
// For non-union types, check if the property exists and has a literal type
const type = declaredType.flags & TypeFlags.Union && isTypeSubsetOf(computedType, declaredType) ? declaredType : computedType;
const propType = getTypeOfPropertyOfType(type, name);
if (propType && isUnitLikeType(propType)) {
return access;
}
}
}
// This enables narrowing to never when all possibilities are eliminated
else {
const access = getCandidateDiscriminantPropertyAccess(expr);
if (access) {
const name = getAccessedPropertyName(access);
if (name) {
// For non-union types, check if the property exists and has a literal type
const type = declaredType.flags & TypeFlags.Union && isTypeSubsetOf(computedType, declaredType) ? declaredType : computedType;
// Only try to get property type for safe types (avoid EvolvingArray and other special types)
if (type.flags & TypeFlags.Object && !((type as ObjectType).objectFlags & ObjectFlags.EvolvingArray)) {
const propType = getTypeOfPropertyOfType(type, name);
if (propType && isUnitLikeType(propType)) {
return access;
}
}
}
}
}
return undefined;
}