diff --git a/src/services/stringCompletions.ts b/src/services/stringCompletions.ts index cb7260f6d8068..922e50654bbe1 100644 --- a/src/services/stringCompletions.ts +++ b/src/services/stringCompletions.ts @@ -241,7 +241,7 @@ namespace ts.Completions.StringCompletions { // Get string literal completions from specialized signatures of the target // i.e. declare function f(a: 'A'); // f("/*completion position*/") - return argumentInfo ? getStringLiteralCompletionsFromSignature(argumentInfo.invocation, node, argumentInfo, typeChecker) : fromContextualType(); + return argumentInfo && getStringLiteralCompletionsFromSignature(argumentInfo.invocation, node, argumentInfo, typeChecker) || fromContextualType(); } // falls through (is `require("")` or `require(""` or `import("")`) @@ -283,7 +283,7 @@ namespace ts.Completions.StringCompletions { type !== current && isLiteralTypeNode(type) && isStringLiteral(type.literal) ? type.literal.text : undefined); } - function getStringLiteralCompletionsFromSignature(call: CallLikeExpression, arg: StringLiteralLike, argumentInfo: SignatureHelp.ArgumentInfoForCompletions, checker: TypeChecker): StringLiteralCompletionsFromTypes { + function getStringLiteralCompletionsFromSignature(call: CallLikeExpression, arg: StringLiteralLike, argumentInfo: SignatureHelp.ArgumentInfoForCompletions, checker: TypeChecker): StringLiteralCompletionsFromTypes | undefined { let isNewIdentifier = false; const uniques = new Map(); const candidates: Signature[] = []; @@ -301,8 +301,7 @@ namespace ts.Completions.StringCompletions { isNewIdentifier = isNewIdentifier || !!(type.flags & TypeFlags.String); return getStringLiteralTypes(type, uniques); }); - - return { kind: StringLiteralCompletionKind.Types, types, isNewIdentifier }; + return length(types) ? { kind: StringLiteralCompletionKind.Types, types, isNewIdentifier } : undefined; } function stringLiteralCompletionsFromProperties(type: Type | undefined): StringLiteralCompletionsFromProperties | undefined { diff --git a/tests/cases/fourslash/completionForStringLiteralFromSignature2.ts b/tests/cases/fourslash/completionForStringLiteralFromSignature2.ts index f070d7e238902..4cc472ba173d0 100644 --- a/tests/cases/fourslash/completionForStringLiteralFromSignature2.ts +++ b/tests/cases/fourslash/completionForStringLiteralFromSignature2.ts @@ -4,4 +4,4 @@ ////declare function f(a: string, b: number): void; ////f("/**/", 0); -verify.completions({ marker: "", exact: [], isNewIdentifierLocation: true }); +verify.completions({ marker: "", exact: [], isNewIdentifierLocation: false }); diff --git a/tests/cases/fourslash/stringLiteralCompletionsInJsxAttributeInitializer.ts b/tests/cases/fourslash/stringLiteralCompletionsInJsxAttributeInitializer.ts new file mode 100644 index 0000000000000..6e19605d7d064 --- /dev/null +++ b/tests/cases/fourslash/stringLiteralCompletionsInJsxAttributeInitializer.ts @@ -0,0 +1,11 @@ +/// + +// @jsx: preserve +// @filename: /a.tsx +////type Props = { a: number } | { b: "somethingelse" }; +////declare function Foo(args: Props): any +//// +////const a1 = +////const a2 = + +verify.completions({ marker: ["1", "2"], exact: ["somethingelse"] });