Skip to content

Fixed string completions from generic conditional types that match typed argument against template literal type #52997

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

Conversation

Andarist
Copy link
Contributor

fixes #49680 , cc @andrewbranch

@typescript-bot typescript-bot added the For Milestone Bug PRs that fix a bug with a specific milestone label Feb 27, 2023
return type.isUnion() ? flatMap(type.types, t => getStringLiteralTypes(t, uniques)) :
type.isStringLiteral() && !(type.flags & TypeFlags.EnumLiteral) && addToSeen(uniques, type.value) ? [type] : emptyArray;
return type.isUnion() ? flatMap(type.types, t => getStringLiteralTypes(t, uniques, alreadyTyped)) :
type.isStringLiteral() && !(type.flags & TypeFlags.EnumLiteral) && type.value.startsWith(alreadyTyped) && addToSeen(uniques, type.value) ? [type] : emptyArray;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Based on those few test failures it seems that I can't just simply discard the already matched strings at this stage. Those are still useful for creating replacement spans here:

replacementSpan: getReplacementSpanForContextToken(contextToken)

However, I'm not exactly sure what is the relation between what is being returned by TS and what is displayed by VS Code. VS code tends to skip some completions... like even if we take a look at the added test case: before this change TS returns "a" | "b" but VS Code doesn't display anything.

The goal behind this change here was for the logic to fallback to the fromContextualType() here:

return argumentInfo && getStringLiteralCompletionsFromSignature(argumentInfo.invocation, node, argumentInfo, typeChecker) || fromContextualType();

I tried to avoid doing both requests unconditionally and concatenating their results but perhaps that is the way to go here? Or perhaps this filtering should happen on a different level (like before doing the second request) but it doesn't affect the returned types? Maybe it should just test out if anything "interesting" was returned from the first request?

@Andarist
Copy link
Contributor Author

Andarist commented Jan 7, 2024

This was superseded by #54121

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
For Milestone Bug PRs that fix a bug with a specific milestone
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

with infer in template string, suggestion works in 4.6, but not work in 4.7
3 participants