mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-04-12 23:36:28 -05:00
Fix EvolvingArray crash in discriminant property narrowing
Co-authored-by: RyanCavanaugh <6685088+RyanCavanaugh@users.noreply.github.com>
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user