fix: report error for private property access on generic intersection types#63548
Closed
hesam-oxe wants to merge 3 commits into
Closed
fix: report error for private property access on generic intersection types#63548hesam-oxe wants to merge 3 commits into
hesam-oxe wants to merge 3 commits into
Conversation
… types Added check in getIndexedAccessTypeOrUndefined to detect conflicting private properties in intersection types before resolving indexed access. Closes microsoft#62294
Contributor
There was a problem hiding this comment.
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
undefinedis currently gated onaccessNode, which means the checker will continue resolving the indexed access whenaccessNodeis absent (e.g., in non-error-reporting/type-query contexts), potentially producing a type that still reflects access to conflicting private members. Consider returningundefinedwheneverconflictingPrivateis found, and only conditionally emittingerror(...)whenaccessNodeis present.
src/compiler/checker.ts:1 - The non-null assertion on
getDeclaringClass(conflictingPrivate)!makes this path brittle ifisConflictingPrivatePropertycan 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.
Author
|
@microsoft-github-policy-service agree |
Author
|
@RyanCavanaugh Ah, I missed the memo about typescript-go! I'll re-implement this fix in the typescript-go repo instead. Quick question: is the same |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
getIndexedAccessTypeOrUndefineddid not check for conflictingprivate properties in intersection types before resolving indexed access.
Fix
Added check after
getReducedType()using existingisConflictingPrivatePropertyhelper to detect private property conflicts and report
Property_0_is_private_and_only_accessible_within_class_1.Before
After
Related
Closes #62294