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

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/services/stringCompletions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -495,7 +495,7 @@ function getStringLiteralCompletionsFromSignature(call: CallLikeExpression, arg:
}
}
isNewIdentifier = isNewIdentifier || !!(type.flags & TypeFlags.String);
return getStringLiteralTypes(type, uniques);
return getStringLiteralTypes(type, uniques, arg.text);
});
return length(types) ? { kind: StringLiteralCompletionKind.Types, types, isNewIdentifier } : undefined;
}
Expand Down Expand Up @@ -527,11 +527,11 @@ function stringLiteralCompletionsForObjectLiteral(checker: TypeChecker, objectLi
};
}

function getStringLiteralTypes(type: Type | undefined, uniques = new Map<string, true>()): readonly StringLiteralType[] {
function getStringLiteralTypes(type: Type | undefined, uniques = new Map<string, true>(), alreadyTyped = ""): readonly StringLiteralType[] {
if (!type) return emptyArray;
type = skipConstraint(type);
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?

}

interface NameAndKind {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/// <reference path="fourslash.ts" />

// @Filename: /a.tsx
//// type PathOf<T, K extends string, P extends string = ""> =
//// K extends `${infer U}.${infer V}`
//// ? U extends keyof T ? PathOf<T[U], V, `${P}${U}.`> : `${P}${keyof T & (string | number)}`
//// : K extends keyof T ? `${P}${K}` : `${P}${keyof T & (string | number)}`;
////
//// declare function consumer<K extends string>(path: PathOf<{a: string, b: {c: string}}, K>) : number;
////
//// consumer('b./*ts*/')

verify.completions({ marker: ["ts"], exact: ["b.c"] });