Skip to content

Commit a7028cd

Browse files
committed
fix: report error for private property access on generic intersection types
Added check in getIndexedAccessTypeOrUndefined to detect conflicting private properties in intersection types before resolving indexed access. Closes #62294
1 parent f350b52 commit a7028cd

1 file changed

Lines changed: 11 additions & 0 deletions

File tree

src/compiler/checker.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19656,6 +19656,17 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
1965619656
return wildcardType;
1965719657
}
1965819658
objectType = getReducedType(objectType);
19659+
// If the object type is an intersection with conflicting private properties,
19660+
// report an error — we shouldn't be able to access those properties via T["key"]
19661+
if (objectType.flags & TypeFlags.Intersection && !(objectType.flags & TypeFlags.Never)) {
19662+
const props = getPropertiesOfUnionOrIntersectionType(objectType as IntersectionType);
19663+
const conflictingPrivate = find(props, isConflictingPrivateProperty);
19664+
if (conflictingPrivate && accessNode) {
19665+
error(accessNode, Diagnostics.Property_0_is_private_and_only_accessible_within_class_1,
19666+
symbolToString(conflictingPrivate), typeToString(getDeclaringClass(conflictingPrivate)!));
19667+
return undefined;
19668+
}
19669+
}
1965919670
// If the object type has a string index signature and no other members we know that the result will
1966019671
// always be the type of that index signature and we can simplify accordingly.
1966119672
if (isStringIndexSignatureOnlyType(objectType) && !(indexType.flags & TypeFlags.Nullable) && isTypeAssignableToKind(indexType, TypeFlags.String | TypeFlags.Number)) {

0 commit comments

Comments
 (0)