diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 333f0ccbcfd..945ae034b25 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -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; }