Skip to content

Enhance type argument completions #62170

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

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

mkantor
Copy link
Contributor

@mkantor mkantor commented Aug 1, 2025

#43526 implemented limited support for completions within type arguments, but only for type arguments of types (not values like generic function calls), and only for specific locations within the type literal (e.g. property names of object types). This pull request generalizes type argument completions such that suggestions now appear in many more scenarios, including:

  • Type arguments of expressions:
    • function calls (f<…>())
    • new expressions (new Foo<…>())
    • tagged templates (tag<…>`blah`)
    • JSX elements (<Component<…>/>)
    • decorators (@decorator<…> class {})
    • instantiation expressions (f<…>)
  • Types of property values within type arguments (Foo<{ x: … }>)
  • String literal types (Foo<"…">)
  • Tuple types (Foo<[…]>)

Suggested completions are derived from the relevant type parameter's constraint.

This pull request subsumes #61758 and includes the additional enhancements mentioned in that pull request's description.

Fixes #61751.
Fixes #56299.
Fixes #52898.
Fixes #34771.

mkantor added 2 commits August 1, 2025 07:33
Previously, `getTypeArgumentConstraint` could only find constraints for
type arguments of generic type instantiations. Now it additionally
supports type arguments of the following expression kinds:

- function calls
- `new` expressions
- tagged templates
- JSX expressions
- decorators
- instantiation expressions
@i-ayushh18
Copy link

🤖 AI Assistant: Task completed: PR #62170: Enhance type argument completions

URL: #6217...

1 similar comment
@i-ayushh18
Copy link

🤖 AI Assistant: Task completed: PR #62170: Enhance type argument completions

URL: #6217...

@mkantor
Copy link
Contributor Author

mkantor commented Aug 4, 2025

@i-ayushh18 I don't know if this is intentional, but it seems that you have a bot which is spamming a bunch of nonsense comments across many pull requests. If it is intentional, please cut it out.

@i-ayushh18
Copy link

@i-ayushh18 I don't know if this is intentional, but it seems that you have a bot which is spamming a bunch of nonsense comments across many pull requests. If it is intentional, please cut it out.

Sorry for the thing,a bot was responsible for that,will suspend that at latest!

@edperetokin
Copy link

I have tested this changes locally, and it works well for me.

Looking forward to seeing it merged and released soon.

return constraint && instantiateType(constraint, createTypeMapper(typeParameters, getEffectiveTypeArguments(typeReferenceNode, typeParameters)));
let typeArgumentPosition;
if (
"typeArguments" in node.parent && // eslint-disable-line local/no-in-operator
Copy link
Member

Choose a reason for hiding this comment

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

We typically avoid these kinds of checks; is there no helper that achieves the same thing? Or at least, a switch case?

Copy link
Contributor Author

@mkantor mkantor Aug 13, 2025

Choose a reason for hiding this comment

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

I was originally calling hasTypeArguments here, but noticed that it's buggy and dropped it. For details, see the commit message of 53d3f29 and the commentary in #61758 (comment) (note that 53d3f29 isn't included in this branch).

However, some code change I made in the meantime must have sidestepped those issues, as I just tried hasTypeArguments here again and everything seems copacetic.

I'll push a commit switching back to hasTypeArguments, but since we're on the subject: should anything be done about its sketchiness? Maybe it's made moot by the Go rewrite (where a hasTypeArguments equivalent would presumably work differently).

Copy link
Member

Choose a reason for hiding this comment

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

hasTypeArguments doesn't even exist in the Go codebase. Notably hasTypeArguments is actually entirely unused in this repo, so I think nobody has ever even noticed it's broken and tried to fix it.

@@ -42624,6 +42624,48 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
return undefined;
}

function getSignaturesFromCallLike(node: CallLikeExpression): readonly Signature[] {
Copy link
Member

Choose a reason for hiding this comment

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

It feels suspicious that this is the first time we'd need a func like this; there's nothing else that does this? Not just getResolvedSignature?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I need the original un-instantiated generic signatures, but getResolvedSignature always gives me concrete ones, even with CheckMode.SkipGenericFunctions.

When asking for completions there is a (possibly-incomplete) type argument already present (the node looks something like f<{}>()). I tried artificially clearing out the type argument list before calling getResolvedSignature, but that still gives me concrete signatures (with type parameters substituted for their constraints). I traced the logic for regular function calls and in that case concretion occurs within resolveCall (the call stack is getResolvedSignatureresolveSignatureresolveCallExpressionresolveCall).

I searched for other existing utilities that would meet my needs but came up empty. It's entirely possible I missed something though; I wasn't sure exactly what to look for.

Maybe I could rename getSignaturesFromCallLike to clarify how it's different from getResolvedSignature. Something like getUninstantiatedSignatures, perhaps? A docblock might also help. Let me know if you have any specific suggestions on these fronts.

@typescript-bot typescript-bot added the For Backlog Bug PRs that fix a backlog bug label Aug 13, 2025
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: Not started
5 participants