- Notifications
You must be signed in to change notification settings - Fork13.1k
Fix completions for generic overloads with mixed function/object parameters#62763
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Open
PaulyBearCoding wants to merge1 commit intomicrosoft:mainChoose a base branch fromPaulyBearCoding:fix-62693-generic-overload-completions
base:main
Could not load branches
Branch not found:{{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline, and old review comments may become outdated.
Open
+131 −4
Conversation
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
…metersFixesmicrosoft#62693## ProblemWhen a generic function has overloads with mixed parameter types (function vs object literal), TypeScript shows incorrect completions for object literals. For example:```typescriptdeclare function task<T>(make: () => T): voiddeclare function task<T>(arg: {make: () => T}): voidtask({ // BUG: Shows bind, call, apply instead of "make"})```The editor shows function properties (bind, call, apply) instead of the expected object property "make".## Root CauseDuring completion, generic type inference is blocked (type parameters are unknown). When `chooseOverload` evaluates candidates, it:1. Attempts to resolve the function-type overload first2. Cannot infer `T` during completion context3. Falls back to treating the object literal as a function type4. Returns function properties instead of object propertiesThe issue occurs because overload selection doesn't account for the syntactic context (completing an object literal) when generic inference is unavailable.## SolutionThe fix tracks which argument position is being completed and uses this information during overload resolution to prefer object-type overloads when completing object literals.### Changes**types.ts (line 6311):**Added `completionArgumentIndex` to NodeLinks to track which argument is being completed.**checker.ts (lines 32072-32076):**Store the argument index when inference is blocked during completion:```typescriptif (isInferencePartiallyBlocked) { links.completionArgumentIndex = argIndex;}```**checker.ts (lines 36728-36832):**Enhanced `chooseOverload` to:1. Detect when completing an object literal at argument position 02. Skip overload candidates with function-type parameters3. Accept object-type overload candidates even if the literal is incompleteThis allows the type checker to select the correct overload based on syntactic context rather than relying solely on type inference.## Why This WorksThe fix is surgical and only affects generic overload resolution during completion:- **Targeted scope:** Only triggers for object literals at argument 0 with blocked inference- **Preserved behavior:** Non-generic overloads and other arguments remain unchanged- **Syntactic guidance:** Uses the object literal syntax to disambiguate overloads when type inference is unavailableThe approach respects TypeScript's existing completion infrastructure while providing better overload selection for this specific edge case.## TestingAdded comprehensive test coverage in `completionForGenericOverloadObjectLiteral.ts`:- Generic function-first overload (the bug)- Generic object-first overload (control)- Non-generic overload (control)- Multiple type parameters with optional properties (rigorous)All tests verify that object properties appear in completions and function properties (bind, call, apply) are correctly excluded.Test results: 99,000 passing, 0 regressions
Sign up for freeto 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.
Fixes#62693
Problem
When a generic function has overloads with mixed parameter types (function vs object literal), TypeScript shows incorrect completions for object literals. For example:
The editor shows function properties (bind, call, apply) instead of the expected object property "make".
Root Cause
During completion, generic type inference is blocked (type parameters are unknown). When
chooseOverloadevaluates candidates, it:Tduring completion contextThe issue occurs because overload selection doesn't account for the syntactic context (completing an object literal) when generic inference is unavailable.
Solution
The fix tracks which argument position is being completed and uses this information during overload resolution to prefer object-type overloads when completing object literals.
Changes
types.ts (line 6311):
Added
completionArgumentIndexto NodeLinks to track which argument is being completed.checker.ts (lines 32072-32076):
Store the argument index when inference is blocked during completion:
checker.ts (lines 36728-36832):
Enhanced
chooseOverloadto:This allows the type checker to select the correct overload based on syntactic context rather than relying solely on type inference.
Why This Works
The fix is surgical and only affects generic overload resolution during completion:
The approach respects TypeScript's existing completion infrastructure while providing better overload selection for this specific edge case.
Testing
Added comprehensive test coverage in
completionForGenericOverloadObjectLiteral.ts:All tests verify that object properties appear in completions and function properties (bind, call, apply) are correctly excluded.
Test results: 99,000 passing, 0 regressions