Skip to content

Filter out classes with private members from the contextual types for object literal nodes - #56183

Closed
Mateusz Burzyński (Andarist) wants to merge 8 commits into
microsoft:mainfrom
Andarist:filter-out-classes-with-private-members-for-contextual-type
Closed

Filter out classes with private members from the contextual types for object literal nodes#56183
Mateusz Burzyński (Andarist) wants to merge 8 commits into
microsoft:mainfrom
Andarist:filter-out-classes-with-private-members-for-contextual-type

Conversation

@Andarist

Copy link
Copy Markdown
Contributor

fixes#56177

@typescript-botTypeScript Bot (typescript-bot) added the For Uncommitted Bug PR for untriaged, rejected, closed or missing bug label Oct 23, 2023

function containsNonPublicProperties(props: Symbol[]) {
return some(props, p => !!(getDeclarationModifierFlagsFromSymbol(p) & ModifierFlags.NonPublicAccessibilityModifier));
return some(props, p => !!(getDeclarationModifierFlagsFromSymbol(p) & ModifierFlags.NonPublicAccessibilityModifier) || !!p.valueDeclaration && isNamedDeclaration(p.valueDeclaration) && isPrivateIdentifier(p.valueDeclaration.name));

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

likely this alone (or something close to it) would fix the completion problem (private foo is already filtered out from completions, only #foo is not) but that wouldn't fix the contextuallyTypedParametersPositionIncludesClassWithPrivateMember test cases

Comment threadsrc/compiler/checker.ts
Comment threadsrc/compiler/checker.ts Outdated
@jakebailey

Copy link
Copy Markdown
Member

@typescript-bot

TypeScript Bot (typescript-bot) commented Nov 3, 2023

Copy link
Copy Markdown
Contributor

Heya Jake Bailey (@jakebailey), I've started to run the tarball bundle task on this PR at dcff0c7. You can monitor the build here.

@typescript-bot

TypeScript Bot (typescript-bot) commented Nov 3, 2023

Copy link
Copy Markdown
Contributor

Heya Jake Bailey (@jakebailey), I've started to run the diff-based user code test suite on this PR at dcff0c7. You can monitor the build here.

Update: The results are in!

@typescript-bot

TypeScript Bot (typescript-bot) commented Nov 3, 2023

Copy link
Copy Markdown
Contributor

Heya Jake Bailey (@jakebailey), I've started to run the diff-based user code test suite (tsserver) on this PR at dcff0c7. You can monitor the build here.

Update: The results are in!

@typescript-bot

TypeScript Bot (typescript-bot) commented Nov 3, 2023

Copy link
Copy Markdown
Contributor

Heya Jake Bailey (@jakebailey), I've started to run the diff-based top-repos suite on this PR at dcff0c7. You can monitor the build here.

Update: The results are in!

@typescript-bot

TypeScript Bot (typescript-bot) commented Nov 3, 2023

Copy link
Copy Markdown
Contributor

Heya Jake Bailey (@jakebailey), I've started to run the diff-based top-repos suite (tsserver) on this PR at dcff0c7. You can monitor the build here.

Update: The results are in!

@typescript-bot

TypeScript Bot (typescript-bot) commented Nov 3, 2023

Copy link
Copy Markdown
Contributor

Heya Jake Bailey (@jakebailey), I've started to run the regular perf test suite on this PR at dcff0c7. You can monitor the build here.

Update: The results are in!

@typescript-bot

TypeScript Bot (typescript-bot) commented Nov 3, 2023

Copy link
Copy Markdown
Contributor

Heya Jake Bailey (@jakebailey), I've started to run the parallelized Definitely Typed test suite on this PR at dcff0c7. You can monitor the build here.

Update: The results are in!

@typescript-bot

TypeScript Bot (typescript-bot) commented Nov 3, 2023

Copy link
Copy Markdown
Contributor

Hey Jake Bailey (@jakebailey), I've packed this into an installable tgz. You can install it for testing by referencing it in your package.json like so:

{
"devDependencies": {
"typescript": "https://typescript.visualstudio.com/cf7ac146-d525-443c-b23c-0d58337efebc/_apis/build/builds/158467/artifacts?artifactName=tgz&fileId=9F3DEB1EDB34D1ED76C56540B3D135C7C7CFAE635A51B5D1FB3D2872C3DD6A4002&fileName=/typescript-5.4.0-insiders.20231103.tgz"
}
}

and then running npm install.


There is also a playground for this build and an npm module you can use via "typescript": "npm:@typescript-deploys/pr-build@5.4.0-pr-56183-8".;

@typescript-bot

Copy link
Copy Markdown
Contributor

Jake Bailey (@jakebailey)
The results of the perf run you requested are in!

Here they are:

Compiler

Comparison Report - baseline..pr
MetricbaselineprDeltaBestWorstp-value
Angular - node (v18.15.0, x64)
Memory used295,152k (± 0.01%)295,119k (± 0.01%)~295,081k295,137kp=0.230 n=6
Parse Time2.63s (± 0.71%)2.62s (± 0.45%)~2.60s2.63sp=0.223 n=6
Bind Time0.84s (± 0.90%)0.83s (± 0.62%)~0.83s0.84sp=0.247 n=6
Check Time8.05s (± 0.40%)8.05s (± 0.18%)~8.03s8.07sp=0.870 n=6
Emit Time7.10s (± 0.30%)7.08s (± 0.24%)~7.05s7.10sp=0.329 n=6
Total Time18.62s (± 0.17%)18.59s (± 0.10%)~18.57s18.62sp=0.061 n=6
Compiler-Unions - node (v18.15.0, x64)
Memory used193,649k (± 1.65%)195,580k (± 1.23%)~190,685k196,656kp=0.471 n=6
Parse Time1.36s (± 0.76%)1.36s (± 0.60%)~1.35s1.37sp=0.932 n=6
Bind Time0.73s (± 0.00%)0.73s (± 0.00%)~0.73s0.73sp=1.000 n=6
Check Time9.18s (± 0.19%)9.17s (± 0.35%)~9.12s9.22sp=0.548 n=6
Emit Time2.63s (± 0.56%)2.63s (± 0.52%)~2.61s2.65sp=0.934 n=6
Total Time13.89s (± 0.21%)13.89s (± 0.19%)~13.84s13.91sp=0.747 n=6
Monaco - node (v18.15.0, x64)
Memory used347,373k (± 0.01%)347,408k (± 0.01%)~347,374k347,430kp=0.066 n=6
Parse Time2.45s (± 0.67%)2.46s (± 0.31%)~2.45s2.47sp=0.216 n=6
Bind Time0.95s (± 1.44%)0.94s (± 0.67%)~0.93s0.95sp=0.388 n=6
Check Time6.93s (± 0.32%)6.93s (± 0.38%)~6.89s6.96sp=0.935 n=6
Emit Time4.05s (± 0.35%)4.06s (± 0.74%)~4.03s4.10sp=0.935 n=6
Total Time14.38s (± 0.23%)14.38s (± 0.32%)~14.34s14.45sp=0.872 n=6
TFS - node (v18.15.0, x64)
Memory used302,595k (± 0.01%)302,576k (± 0.01%)~302,546k302,640kp=0.298 n=6
Parse Time1.99s (± 0.49%)1.99s (± 0.94%)~1.96s2.01sp=0.404 n=6
Bind Time1.00s (± 0.81%)1.00s (± 0.54%)~1.00s1.01sp=0.859 n=6
Check Time6.25s (± 0.31%)6.25s (± 0.28%)~6.23s6.27sp=0.463 n=6
Emit Time3.59s (± 0.46%)3.57s (± 0.72%)~3.55s3.62sp=0.288 n=6
Total Time12.83s (± 0.23%)12.83s (± 0.19%)~12.80s12.87sp=0.684 n=6
material-ui - node (v18.15.0, x64)
Memory used470,547k (± 0.00%)470,578k (± 0.01%)~470,530k470,677kp=0.378 n=6
Parse Time2.56s (± 0.84%)2.56s (± 0.43%)~2.54s2.57sp=0.743 n=6
Bind Time1.00s (± 1.23%)0.99s (± 1.48%)~0.97s1.01sp=0.805 n=6
Check Time16.64s (± 0.53%)16.68s (± 0.26%)~16.63s16.74sp=0.574 n=6
Emit Time0.00s (± 0.00%)0.00s (± 0.00%)~0.00s0.00sp=1.000 n=6
Total Time20.20s (± 0.43%)20.24s (± 0.23%)~20.19s20.30sp=0.520 n=6
xstate - node (v18.15.0, x64)
Memory used512,817k (± 0.01%)512,782k (± 0.01%)~512,719k512,830kp=0.298 n=6
Parse Time3.27s (± 0.33%)3.27s (± 0.16%)~3.27s3.28sp=0.324 n=6
Bind Time1.54s (± 0.49%)1.54s (± 0.68%)~1.53s1.56sp=0.273 n=6
Check Time2.84s (± 0.57%)2.82s (± 0.50%)~2.80s2.84sp=0.053 n=6
Emit Time0.08s (± 0.00%)0.08s (± 0.00%)~0.08s0.08sp=1.000 n=6
Total Time7.73s (± 0.30%)7.71s (± 0.22%)~7.68s7.73sp=0.108 n=6
System info unknown
Hosts
  • node (v18.15.0, x64)
Scenarios
  • Angular - node (v18.15.0, x64)
  • Compiler-Unions - node (v18.15.0, x64)
  • Monaco - node (v18.15.0, x64)
  • TFS - node (v18.15.0, x64)
  • material-ui - node (v18.15.0, x64)
  • xstate - node (v18.15.0, x64)
BenchmarkNameIterations
Currentpr6
Baselinebaseline6

tsserver

Comparison Report - baseline..pr
MetricbaselineprDeltaBestWorstp-value
Compiler-UnionsTSServer - node (v18.15.0, x64)
Req 1 - updateOpen2,381ms (± 0.49%)2,382ms (± 0.48%)~2,369ms2,396msp=1.000 n=6
Req 2 - geterr5,347ms (± 1.38%)5,373ms (± 1.55%)~5,288ms5,451msp=0.521 n=6
Req 3 - references326ms (± 0.50%)329ms (± 1.40%)~322ms334msp=0.293 n=6
Req 4 - navto277ms (± 1.21%)277ms (± 1.27%)~273ms280msp=1.000 n=6
Req 5 - completionInfo count1,356 (± 0.00%)1,356 (± 0.00%)~1,3561,356p=1.000 n=6
Req 5 - completionInfo82ms (± 8.41%)84ms (± 8.91%)~75ms90msp=1.000 n=6
CompilerTSServer - node (v18.15.0, x64)
Req 1 - updateOpen2,512ms (± 1.18%)2,500ms (± 1.16%)~2,451ms2,530msp=0.575 n=6
Req 2 - geterr4,118ms (± 2.08%)4,100ms (± 1.81%)~4,037ms4,206msp=0.936 n=6
Req 3 - references339ms (± 1.58%)340ms (± 1.59%)~333ms345msp=0.570 n=6
Req 4 - navto282ms (± 0.58%)282ms (± 0.27%)~281ms283msp=0.564 n=6
Req 5 - completionInfo count1,518 (± 0.00%)1,518 (± 0.00%)~1,5181,518p=1.000 n=6
Req 5 - completionInfo83ms (± 7.69%)86ms (± 7.27%)~77ms90msp=0.314 n=6
xstateTSServer - node (v18.15.0, x64)
Req 1 - updateOpen2,598ms (± 0.32%)2,596ms (± 0.19%)~2,590ms2,603msp=0.625 n=6
Req 2 - geterr1,697ms (± 2.62%)1,717ms (± 1.94%)~1,673ms1,767msp=0.296 n=6
Req 3 - references117ms (± 7.57%)108ms (± 5.85%)~105ms121msp=0.062 n=6
Req 4 - navto366ms (± 0.22%)365ms (± 0.30%)~363ms366msp=0.383 n=6
Req 5 - completionInfo count2,073 (± 0.00%)2,073 (± 0.00%)~2,0732,073p=1.000 n=6
Req 5 - completionInfo309ms (± 1.02%)309ms (± 2.08%)~299ms316msp=0.936 n=6
System info unknown
Hosts
  • node (v18.15.0, x64)
Scenarios
  • CompilerTSServer - node (v18.15.0, x64)
  • Compiler-UnionsTSServer - node (v18.15.0, x64)
  • xstateTSServer - node (v18.15.0, x64)
BenchmarkNameIterations
Currentpr6
Baselinebaseline6

Startup

Comparison Report - baseline..pr
MetricbaselineprDeltaBestWorstp-value
tsc-startup - node (v18.15.0, x64)
Execution time152.31ms (± 0.17%)152.31ms (± 0.20%)~151.26ms157.39msp=0.368 n=600
tsserver-startup - node (v18.15.0, x64)
Execution time227.74ms (± 0.15%)227.69ms (± 0.17%)-0.05ms (- 0.02%)226.41ms233.22msp=0.002 n=600
tsserverlibrary-startup - node (v18.15.0, x64)
Execution time229.33ms (± 0.17%)229.37ms (± 0.19%)~227.78ms235.10msp=0.625 n=600
typescript-startup - node (v18.15.0, x64)
Execution time229.26ms (± 0.19%)229.22ms (± 0.18%)~227.65ms234.16msp=0.609 n=600
System info unknown
Hosts
  • node (v18.15.0, x64)
Scenarios
  • tsc-startup - node (v18.15.0, x64)
  • tsserver-startup - node (v18.15.0, x64)
  • tsserverlibrary-startup - node (v18.15.0, x64)
  • typescript-startup - node (v18.15.0, x64)
BenchmarkNameIterations
Currentpr6
Baselinebaseline6

Developer Information:

Download Benchmarks

@typescript-bot

Copy link
Copy Markdown
Contributor

Jake Bailey (@jakebailey) Here are the results of running the user test suite comparing main and refs/pull/56183/merge:

There were infrastructure failures potentially unrelated to your change:

  • 3 instances of "Package install failed"

Otherwise...

Everything looks good!

@typescript-bot

Copy link
Copy Markdown
Contributor

Jake Bailey (@jakebailey) Here are the results of running the user test suite comparing main and refs/pull/56183/merge:

Everything looks good!

@typescript-bot

Copy link
Copy Markdown
Contributor

Hey Jake Bailey (@jakebailey), the results of running the DT tests are ready.
Everything looks the same!
You can check the log here.

@typescript-bot

Copy link
Copy Markdown
Contributor

Jake Bailey (@jakebailey) Here are the results of running the top-repos suite comparing main and refs/pull/56183/merge:

Something interesting changed - please have a look.

Details

Server exited prematurely with code unknown and signal SIGABRT

Server exited prematurely with code unknown and signal SIGABRT

Affected repos

calcom/cal.com Raw error text: RepoResults7/calcom.cal.com.rawError.txt in the artifact folder

Last few requests

{"seq":808,"type":"request","command":"getOutliningSpans","arguments":{"file":"@PROJECT_ROOT@/apps/swagger/pages/_app.tsx"}}
{"seq":809,"type":"request","command":"updateOpen","arguments":{"changedFiles":[{"fileName":"@PROJECT_ROOT@/apps/swagger/pages/_app.tsx","textChanges":[{"newText":" //comment","start":{"line":1,"offset":42},"end":{"line":1,"offset":42}}]}],"closedFiles":[],"openFiles":[]}}
{"seq":810,"type":"request","command":"updateOpen","arguments":{"changedFiles":[],"closedFiles":["@PROJECT_ROOT@/apps/api/test/lib/middleware/withMiddleware.test.ts"],"openFiles":[]}}
{"seq":811,"type":"request","command":"updateOpen","arguments":{"changedFiles":[],"closedFiles":[],"openFiles":[{"file":"@PROJECT_ROOT@/apps/web/app/layout.tsx","projectRootPath":"@PROJECT_ROOT@"}]}}

Repro steps

  1. git clone https://github.com/calcom/cal.com --recurse-submodules
  2. In dir cal.com, run git reset --hard d1d50b0d9131e650eab2a88396e130dfb2067da4
  3. In dir cal.com, run yarn install --no-immutable --mode=skip-build
  4. Back in the initial folder, download RepoResults7/calcom.cal.com.replay.txt from the artifact folder
  5. npm install --no-save @typescript/server-replay
  6. npx tsreplay ./cal.com ./calcom.cal.com.replay.txt path/to/tsserver.js
  7. npx tsreplay --help to learn about helpful switches for debugging, logging, etc

@typescript-bot

Copy link
Copy Markdown
Contributor

Jake Bailey (@jakebailey) Here are the results of running the top-repos suite comparing main and refs/pull/56183/merge:

Everything looks good!

@jakebailey

Copy link
Copy Markdown
Member

There's a crash above, but not sure if it's due to this PR or not.

@Andarist

Copy link
Copy Markdown
ContributorAuthor

I really doubt that this crash is related to this PR - but who knows? Could you rerun the extended test suite here before I start investigating this?

@jakebailey

Copy link
Copy Markdown
Member

TypeScript Bot (@typescript-bot) test top200

@typescript-bot

TypeScript Bot (typescript-bot) commented Dec 26, 2023

Copy link
Copy Markdown
Contributor

Heya Jake Bailey (@jakebailey), I've started to run the diff-based top-repos suite on this PR at 34d43f0. You can monitor the build here.

Update: The results are in!

@typescript-bot

Copy link
Copy Markdown
Contributor

Jake Bailey (@jakebailey) Here are the results of running the top-repos suite comparing main and refs/pull/56183/merge:

Everything looks good!

@jakebaileyJake Bailey (jakebailey) left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I'm a little bit suspicious of the fix itself from the PoV of just adding another layer of workers and then filtering, as compared to doing this more deeply in getContextualType, but I can't quite put my finger on what is better.

Comment threadsrc/compiler/checker.ts Outdated
@Andarist

Copy link
Copy Markdown
ContributorAuthor

I'm a little bit suspicious of the fix itself from the PoV of just adding another layer of workers and then filtering, as compared to doing this more deeply in getContextualType, but I can't quite put my finger on what is better.

it has been a while since I was working on this. I vaguely remember similar concerns that I had. IIRC, I do this filtering here because I can't access the information about being within "literal expression object" context at a deeper level. At the end of the day, the same operation would have to be done deeper - we get a set of values and we need to reject some. So this level works as OK as any other (as long as it doesn't introduce problems, ofc).

CopilotAI review requested due to automatic review settings August 22, 2025 09:08

CopilotAI 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

This PR fixes an issue with TypeScript's type inference for object literals when classes with private members are part of union types. The fix ensures that when TypeScript provides contextual typing for object literals, classes containing private members are filtered out from consideration, preventing incorrect inference that would include inaccessible private properties.

Key changes:

  • Enhanced type discrimination logic to exclude classes with private members from contextual typing
  • Updated completion logic to detect both traditional private members and ES private fields (#property)
  • Added comprehensive test coverage for various scenarios involving private members

Reviewed Changes

Copilot reviewed 14 out of 14 changed files in this pull request and generated no comments.

Show a summary per file
FileDescription
src/compiler/checker.tsCore fix - filters out classes with private members during type discrimination for contextual typing
src/services/completions.tsEnhanced detection of non-public properties to include ES private fields alongside traditional private members
tests/cases/compiler/contextuallyTypedParametersPositionIncludesClassWithPrivateMember1.tsTest case for ES private fields (#property) in contextual typing scenarios
tests/cases/compiler/contextuallyTypedParametersPositionIncludesClassWithPrivateMember2.tsTest case for traditional private members in contextual typing scenarios
tests/cases/fourslash/completionsObjectLiteralPositionIncludesClassWithPrivateMember1.tsFourslash test for ES private fields in object literal completions
tests/cases/fourslash/completionsObjectLiteralPositionIncludesClassWithPrivateMember2.tsFourslash test for ES private fields in object literal completions (nested object variant)
tests/cases/fourslash/completionsObjectLiteralPositionIncludesClassWithPrivateMember3.tsFourslash test for traditional private members in object literal completions
tests/cases/fourslash/completionsObjectLiteralPositionIncludesClassWithPrivateMember4.tsFourslash test for traditional private members in object literal completions (nested object variant)
tests/baselines/reference/*Generated baseline files showing expected compilation results and error messages
Comments suppressed due to low confidence (1)

@Andarist

Copy link
Copy Markdown
ContributorAuthor

I'm a little bit suspicious of the fix itself from the PoV of just adding another layer of workers and then filtering, as compared to doing this more deeply in getContextualType, but I can't quite put my finger on what is better.

I think you were right. From what I can tell, this can just easily be put into discriminateTypeByDiscriminableItems - I pushed out this change.

@typescript-bot

Copy link
Copy Markdown
Contributor

With 6.0 out as the final release vehicle for this codebase, we're closing all PRs that don't fit the merge criteria for post-6.0 patches. If you think this was a mistake and this PR fits the post-6.0 patch criteria, please post to the 6.0 iteration issue with details (specifically, which PR and which patch criteria it satisfies).

Next steps for PRs:

  • For crash bugfixes or language service improvements, PRs are currently accepted at the typescript-go repo
  • Changes to type system behavior should wait until after 7.0, at which point mainline TypeScript development will resume in this repository with the Go codebase
  • Library file updates (lib.d.ts etc) continue to live in this repo or the DOM Generator repo as appropriate

@github-project-automationgithub-project-automationBot moved this from Waiting on reviewers to Done in PR BacklogMar 24, 2026
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

For Backlog BugPRs that fix a backlog bug

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

Don't suggest private fields in contexts where they are out of scope

7 participants

@Andarist@jakebailey@typescript-bot@sandersn@RyanCavanaugh@iisaduan