Skip to content

Commit cf68a12

Browse files
authored
Improve string literal completions for property values when a partially-typed string fixes inference to a type parameter (#51770)
1 parent 5d8ef4b commit cf68a12

File tree

3 files changed

+15
-5
lines changed

3 files changed

+15
-5
lines changed

src/services/stringCompletions.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,7 @@ function getStringLiteralCompletionEntries(sourceFile: SourceFile, node: StringL
421421
function fromContextualType(): StringLiteralCompletion {
422422
// Get completion for string literal from string literal type
423423
// i.e. var x: "hi" | "hello" = "/*completion position*/"
424-
return { kind: StringLiteralCompletionKind.Types, types: getStringLiteralTypes(getContextualTypeFromParent(node, typeChecker)), isNewIdentifier: false };
424+
return { kind: StringLiteralCompletionKind.Types, types: getStringLiteralTypes(getContextualTypeFromParent(node, typeChecker, ContextFlags.Completions)), isNewIdentifier: false };
425425
}
426426
}
427427

src/services/utilities.ts

+5-4
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import {
2929
CompilerOptions,
3030
ConditionalExpression,
3131
contains,
32+
ContextFlags,
3233
createPrinter,
3334
createRange,
3435
createScanner,
@@ -3270,21 +3271,21 @@ export function needsParentheses(expression: Expression): boolean {
32703271
}
32713272

32723273
/** @internal */
3273-
export function getContextualTypeFromParent(node: Expression, checker: TypeChecker): Type | undefined {
3274+
export function getContextualTypeFromParent(node: Expression, checker: TypeChecker, contextFlags?: ContextFlags): Type | undefined {
32743275
const { parent } = node;
32753276
switch (parent.kind) {
32763277
case SyntaxKind.NewExpression:
3277-
return checker.getContextualType(parent as NewExpression);
3278+
return checker.getContextualType(parent as NewExpression, contextFlags);
32783279
case SyntaxKind.BinaryExpression: {
32793280
const { left, operatorToken, right } = parent as BinaryExpression;
32803281
return isEqualityOperatorKind(operatorToken.kind)
32813282
? checker.getTypeAtLocation(node === right ? left : right)
3282-
: checker.getContextualType(node);
3283+
: checker.getContextualType(node, contextFlags);
32833284
}
32843285
case SyntaxKind.CaseClause:
32853286
return (parent as CaseClause).expression === node ? getSwitchedType(parent as CaseClause, checker) : undefined;
32863287
default:
3287-
return checker.getContextualType(node);
3288+
return checker.getContextualType(node, contextFlags);
32883289
}
32893290
}
32903291

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/// <reference path="fourslash.ts" />
2+
3+
// @Filename: /a.tsx
4+
//// declare function bar1<P extends "" | "bar" | "baz">(p: { type: P }): void;
5+
////
6+
//// bar1({ type: "/*ts*/" })
7+
////
8+
9+
verify.completions({ marker: ["ts"], exact: ["", "bar", "baz"] });

0 commit comments

Comments
 (0)