Skip to content

Commit 40787a7

Browse files
authored
Improve contextual completions (#53554)
1 parent 4849947 commit 40787a7

7 files changed

+46
-1
lines changed

src/services/completions.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2938,7 +2938,7 @@ function getContextualType(previousToken: Node, position: number, sourceFile: So
29382938
isEqualityOperatorKind(previousToken.kind) && isBinaryExpression(parent) && isEqualityOperatorKind(parent.operatorToken.kind) ?
29392939
// completion at `x ===/**/` should be for the right side
29402940
checker.getTypeAtLocation(parent.left) :
2941-
checker.getContextualType(previousToken as Expression);
2941+
checker.getContextualType(previousToken as Expression, ContextFlags.Completions) || checker.getContextualType(previousToken as Expression);
29422942
}
29432943
}
29442944

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/// <reference path="fourslash.ts" />
2+
3+
//// declare function test<P extends "a" | "b">(p: P): void;
4+
////
5+
//// test(/*ts*/)
6+
////
7+
8+
verify.completions({ marker: ["ts"], includes: ['"a"', '"b"'], isNewIdentifierLocation: true });
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/// <reference path="fourslash.ts" />
2+
3+
//// declare function test<T extends 'a' | 'b'>(a: { foo: T[] }): void
4+
////
5+
//// test({ foo: [/*ts*/] })
6+
7+
verify.completions({ marker: ["ts"], includes: ['"a"', '"b"'], isNewIdentifierLocation: true });
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/// <reference path="fourslash.ts" />
2+
3+
//// declare function test<T extends 'a' | 'b'>(a: { foo: T[] }): void
4+
////
5+
//// test({ foo: ['a', /*ts*/] })
6+
7+
verify.completions({ marker: ["ts"], includes: ['"a"', '"b"'], isNewIdentifierLocation: true });
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/// <reference path="fourslash.ts" />
2+
3+
//// declare function test<P extends "a" | "b">(p: { type: P }): void;
4+
////
5+
//// test({ type: /*ts*/ })
6+
7+
verify.completions({ marker: ["ts"], includes: ['"a"', '"b"'], isNewIdentifierLocation: false });

tests/cases/fourslash/completionsLiteralFromInferenceWithinInferredType1.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,13 @@
1313
//// b: "/*ts*/",
1414
//// },
1515
//// });
16+
////
17+
//// test({
18+
//// foo: {},
19+
//// bar: {
20+
//// b: /*ts2*/,
21+
//// },
22+
//// });
1623

1724
verify.completions({ marker: ["ts"], exact: ["foo", "bar"] });
25+
verify.completions({ marker: ["ts2"], includes: ['"foo"', '"bar"'], isNewIdentifierLocation: false });

tests/cases/fourslash/completionsLiteralFromInferenceWithinInferredType3.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,13 @@
1212
//// b: ["/*ts*/"],
1313
//// },
1414
//// });
15+
////
16+
//// test({
17+
//// foo: {},
18+
//// bar: {
19+
//// b: [/*ts2*/],
20+
//// },
21+
//// });
1522

1623
verify.completions({ marker: ["ts"], exact: ["foo", "bar"] });
24+
verify.completions({ marker: ["ts2"], includes: ['"foo"', '"bar"'], isNewIdentifierLocation: true });

0 commit comments

Comments
 (0)