-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Filter out self-fulfilling object literal member completions #35709
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
/// <reference path="fourslash.ts" /> | ||
|
||
////interface Test { | ||
//// keyPath?: string; | ||
//// autoIncrement?: boolean; | ||
////} | ||
//// | ||
////function test<T extends Record<string, Test>>(opt: T) { } | ||
//// | ||
////test({ | ||
//// a: { | ||
//// keyPath: '', | ||
//// a/**/ | ||
//// } | ||
////}) | ||
|
||
verify.completions({ | ||
marker: "", | ||
exact: [{ | ||
name: "autoIncrement", | ||
sortText: completion.SortText.OptionalMember | ||
}] | ||
}); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
/// <reference path="fourslash.ts" /> | ||
|
||
////function f1<T>(x: T) {} | ||
////f1({ abc/*1*/ }); | ||
//// | ||
////function f2<T extends { xyz: number }>(x: T) {} | ||
////f2({ x/*2*/ }); | ||
|
||
|
||
verify.completions({ | ||
marker: "1", | ||
exact: [] | ||
}); | ||
|
||
verify.completions({ | ||
marker: "2", | ||
exact: ["xyz"] | ||
}); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/// <reference path="fourslash.ts" /> | ||
|
||
////function f<T extends { x: number }>(p: T & (T extends { hello: string } ? { goodbye: number } : {})) {} | ||
////f({ x/*x*/: 0, hello/*hello*/: "", goodbye/*goodbye*/: 0, abc/*abc*/: "" }) | ||
|
||
|
||
verify.completions({ | ||
marker: "x", | ||
exact: ["x"] | ||
}); | ||
|
||
verify.completions({ | ||
marker: "hello", | ||
exact: [] | ||
}); | ||
|
||
verify.completions({ | ||
marker: "goodbye", | ||
exact: ["goodbye"] | ||
}); | ||
|
||
verify.completions({ | ||
marker: "abc", | ||
exact: [] | ||
}); |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This change looks great but do you think we should allow
abc
in completions ifabc
is some variable in scope?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right, I guess don't filter our anything that could be a shorthand property declaration.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Those were never actually provided as object literal member completions, so this PR doesn’t filter them per se. I’m not sure if anything is really needed here as those completions already come up as plain text matches (I think this is provided by Monaco / VS):
I’m not sure if it’s right to make all in-scope identifiers first-class completions. Real missing members from the contextual type are a very strong signal that the user will want to add those members, but the presence of random identifiers is a very weak signal. We can sort them appropriately, but otherwise there’s no visual distinction that one completion is a sure thing and the other is a wild guess. I think this would get really confusing.
Besides, as I learned in #34855, with generics it can be extremely difficult to know ahead of time whether some hypothetical arbitrary member is a valid completion or not. It often appears not to be until the user adds it, when inference runs and incorporates it into the contextual type just because it’s there. But with conditional types, we effectively can’t know whether that member is going to be valid without speculatively type checking the change, which is prohibitively expensive.