Skip to content

Commit 44ce3cf

Browse files
authored
fix(50224): Intellisense for strings within a type's Union doesn't work properly for JSX (#50231)
* fix(50224): show string literal completions in JsxAttributeInitializer * add feedback changes
1 parent 6ee5db9 commit 44ce3cf

File tree

3 files changed

+15
-5
lines changed

3 files changed

+15
-5
lines changed

src/services/stringCompletions.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ namespace ts.Completions.StringCompletions {
241241
// Get string literal completions from specialized signatures of the target
242242
// i.e. declare function f(a: 'A');
243243
// f("/*completion position*/")
244-
return argumentInfo ? getStringLiteralCompletionsFromSignature(argumentInfo.invocation, node, argumentInfo, typeChecker) : fromContextualType();
244+
return argumentInfo && getStringLiteralCompletionsFromSignature(argumentInfo.invocation, node, argumentInfo, typeChecker) || fromContextualType();
245245
}
246246
// falls through (is `require("")` or `require(""` or `import("")`)
247247

@@ -283,7 +283,7 @@ namespace ts.Completions.StringCompletions {
283283
type !== current && isLiteralTypeNode(type) && isStringLiteral(type.literal) ? type.literal.text : undefined);
284284
}
285285

286-
function getStringLiteralCompletionsFromSignature(call: CallLikeExpression, arg: StringLiteralLike, argumentInfo: SignatureHelp.ArgumentInfoForCompletions, checker: TypeChecker): StringLiteralCompletionsFromTypes {
286+
function getStringLiteralCompletionsFromSignature(call: CallLikeExpression, arg: StringLiteralLike, argumentInfo: SignatureHelp.ArgumentInfoForCompletions, checker: TypeChecker): StringLiteralCompletionsFromTypes | undefined {
287287
let isNewIdentifier = false;
288288
const uniques = new Map<string, true>();
289289
const candidates: Signature[] = [];
@@ -301,8 +301,7 @@ namespace ts.Completions.StringCompletions {
301301
isNewIdentifier = isNewIdentifier || !!(type.flags & TypeFlags.String);
302302
return getStringLiteralTypes(type, uniques);
303303
});
304-
305-
return { kind: StringLiteralCompletionKind.Types, types, isNewIdentifier };
304+
return length(types) ? { kind: StringLiteralCompletionKind.Types, types, isNewIdentifier } : undefined;
306305
}
307306

308307
function stringLiteralCompletionsFromProperties(type: Type | undefined): StringLiteralCompletionsFromProperties | undefined {

tests/cases/fourslash/completionForStringLiteralFromSignature2.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@
44
////declare function f(a: string, b: number): void;
55
////f("/**/", 0);
66

7-
verify.completions({ marker: "", exact: [], isNewIdentifierLocation: true });
7+
verify.completions({ marker: "", exact: [], isNewIdentifierLocation: false });
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/// <reference path="fourslash.ts" />
2+
3+
// @jsx: preserve
4+
// @filename: /a.tsx
5+
////type Props = { a: number } | { b: "somethingelse" };
6+
////declare function Foo(args: Props): any
7+
////
8+
////const a1 = <Foo b={"/*1*/"} />
9+
////const a2 = <Foo b="/*2*/" />
10+
11+
verify.completions({ marker: ["1", "2"], exact: ["somethingelse"] });

0 commit comments

Comments
 (0)