Skip to content

fix: report error for private property access on generic intersection types#63548

Closed
hesam-oxe wants to merge 3 commits into
microsoft:mainfrom
hesam-oxe:fix/private-intersection-error
Closed

fix: report error for private property access on generic intersection types#63548
hesam-oxe wants to merge 3 commits into
microsoft:mainfrom
hesam-oxe:fix/private-intersection-error

Conversation

@hesam-oxe

Copy link
Copy Markdown

When two classes have conflicting private properties, accessing them
via indexed access on their intersection should report an error,
consistent with union behavior.

Root Cause

getIndexedAccessTypeOrUndefined did not check for conflicting
private properties in intersection types before resolving indexed access.

Fix

Added check after getReducedType() using existing isConflictingPrivateProperty
helper to detect private property conflicts and report
Property_0_is_private_and_only_accessible_within_class_1.

Before

type Z<T extends A & B> = T["a"];         // no error ❌
type Z3<T extends A, T2 extends B> = (T & T2)["a"];  // no error ❌

After

type Z<T extends A & B> = T["a"];         // Error ✅
type Z3<T extends A, T2 extends B> = (T & T2)["a"];  // Error ✅

Related

Closes #62294

… types

Added check in getIndexedAccessTypeOrUndefined to detect conflicting private properties in intersection types before resolving indexed access. Closes microsoft#62294
Copilot AI review requested due to automatic review settings June 11, 2026 16:46
@github-project-automation github-project-automation Bot moved this to Not started in PR Backlog Jun 11, 2026
@typescript-automation typescript-automation Bot added For Backlog Bug PRs that fix a backlog bug labels Jun 11, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Note

Copilot was unable to run its full agentic suite in this review.

Adds a type-checking error when using indexed access (T["key"]) on intersection types that contain conflicting private properties, preventing private members from being accessed through the type system.

Changes:

  • Detect conflicting private properties on intersection object types during indexed access.
  • Report an existing “private and only accessible within class” diagnostic for the conflicting property.
  • Bail out of indexed-access resolution when such a conflict is found.
Comments suppressed due to low confidence (2)

src/compiler/checker.ts:1

  • Returning undefined is currently gated on accessNode, which means the checker will continue resolving the indexed access when accessNode is absent (e.g., in non-error-reporting/type-query contexts), potentially producing a type that still reflects access to conflicting private members. Consider returning undefined whenever conflictingPrivate is found, and only conditionally emitting error(...) when accessNode is present.
    src/compiler/checker.ts:1
  • The non-null assertion on getDeclaringClass(conflictingPrivate)! makes this path brittle if isConflictingPrivateProperty can ever match a symbol without a declaring class (e.g., unusual synthetic symbols). Prefer guarding and producing a safe fallback (or skipping the diagnostic parameter) instead of asserting, so this error-reporting path can't crash the checker.

@hesam-oxe

Copy link
Copy Markdown
Author

@microsoft-github-policy-service agree

@RyanCavanaugh

Copy link
Copy Markdown
Member

@github-project-automation github-project-automation Bot moved this from Not started to Done in PR Backlog Jun 11, 2026
@hesam-oxe

Copy link
Copy Markdown
Author

@RyanCavanaugh Ah, I missed the memo about typescript-go!
Thanks for the pointer.

I'll re-implement this fix in the typescript-go repo instead.

Quick question: is the same getIndexedAccessTypeOrUndefined
logic in the Go codebase, or has the checker been restructured?
Just want to know where to start looking. 🙏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

For Backlog Bug PRs that fix a backlog bug

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

Missing error about inability to access conflicting private properties on generic types involving intersections

3 participants