Skip to content

Improve string literal completions for property values when a partially-typed string fixes inference to a type parameter #51770

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 1 commit into from
Dec 20, 2022
Merged
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
2 changes: 1 addition & 1 deletion src/services/stringCompletions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,7 @@ function getStringLiteralCompletionEntries(sourceFile: SourceFile, node: StringL
function fromContextualType(): StringLiteralCompletion {
// Get completion for string literal from string literal type
// i.e. var x: "hi" | "hello" = "/*completion position*/"
return { kind: StringLiteralCompletionKind.Types, types: getStringLiteralTypes(getContextualTypeFromParent(node, typeChecker)), isNewIdentifier: false };
return { kind: StringLiteralCompletionKind.Types, types: getStringLiteralTypes(getContextualTypeFromParent(node, typeChecker, ContextFlags.Completions)), isNewIdentifier: false };
}
}

Expand Down
9 changes: 5 additions & 4 deletions src/services/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import {
CompilerOptions,
ConditionalExpression,
contains,
ContextFlags,
createPrinter,
createRange,
createScanner,
Expand Down Expand Up @@ -3277,21 +3278,21 @@ export function needsParentheses(expression: Expression): boolean {
}

/** @internal */
export function getContextualTypeFromParent(node: Expression, checker: TypeChecker): Type | undefined {
export function getContextualTypeFromParent(node: Expression, checker: TypeChecker, contextFlags?: ContextFlags): Type | undefined {
const { parent } = node;
switch (parent.kind) {
case SyntaxKind.NewExpression:
return checker.getContextualType(parent as NewExpression);
return checker.getContextualType(parent as NewExpression, contextFlags);
case SyntaxKind.BinaryExpression: {
const { left, operatorToken, right } = parent as BinaryExpression;
return isEqualityOperatorKind(operatorToken.kind)
? checker.getTypeAtLocation(node === right ? left : right)
: checker.getContextualType(node);
: checker.getContextualType(node, contextFlags);
}
case SyntaxKind.CaseClause:
return (parent as CaseClause).expression === node ? getSwitchedType(parent as CaseClause, checker) : undefined;
default:
return checker.getContextualType(node);
return checker.getContextualType(node, contextFlags);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The added test case only relies on this one. However, I've passed around contextFlags to all other checker.getContextualType calls in this function for consistency

}
}

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

// @Filename: /a.tsx
//// declare function bar1<P extends "" | "bar" | "baz">(p: { type: P }): void;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

notice how this test is almost identical to the one here:
https://github.com/microsoft/TypeScript/pull/48811/files#diff-a3a2637795672382fb54d54c328d2aff49256e2ce89a12cc7b5e636da2dd48ce

the only difference is that the argument here is p: { type: P } where that linked one uses p: P

////
//// bar1({ type: "/*ts*/" })
////

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