Skip to content

Fix 46457: Improve autocomplete for union of tuples #51421

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

Closed
wants to merge 6 commits into from
Closed
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
18 changes: 17 additions & 1 deletion src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27667,7 +27667,23 @@ namespace ts {
case SyntaxKind.ArrayLiteralExpression: {
const arrayLiteral = parent as ArrayLiteralExpression;
const type = getApparentTypeOfContextualType(arrayLiteral, contextFlags);
return getContextualTypeForElementExpression(type, indexOfNode(arrayLiteral.elements, node));
if (!type) return undefined;
const nodeIndex = indexOfNode(arrayLiteral.elements, node);
let filteredType = type;
if (type.flags & TypeFlags.Union) {
// If there is a union of tuples, filter down to only valid variants of the union
// based on previous elements in the tuple
for(let i = 0; i < nodeIndex; i++) {
const observedType = getTypeOfExpression(arrayLiteral.elements[i]);
if (getObjectFlags(observedType) & ObjectFlags.ArrayLiteral) continue;
filteredType = filterType(filteredType, (t) => {
const variantType = getTypeOfPropertyOfContextualType(t, "" + i as __String);
if (!variantType) return false;
return areTypesComparable(variantType, observedType);
});
}
}
return getContextualTypeForElementExpression(filteredType, nodeIndex);
}
case SyntaxKind.ConditionalExpression:
return getContextualTypeForConditionalOperand(node, contextFlags);
Expand Down
5 changes: 5 additions & 0 deletions tests/cases/fourslash/completionsTuple2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/// <reference path="fourslash.ts" />

////declare const x: ["a", "b"] = ["x", "/**/"]

verify.completions({ marker: "", exact: ["b"] });
5 changes: 5 additions & 0 deletions tests/cases/fourslash/completionsTuple3.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/// <reference path="fourslash.ts" />

////declare const x: ["a", ["b"], "x", ["y"]] = ["a", ["/**/"]]

verify.completions({ marker: "", exact: ["b"] });
6 changes: 6 additions & 0 deletions tests/cases/fourslash/completionsTupleUnion1.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/// <reference path="fourslash.ts" />

////declare const a: "a";
////declare const x: ['a', 'b'] | ['a', 'c'] | ['d', 'e'] = [a, "/**/"];

verify.completions({ marker: "", exact: ["b", "c"] });
6 changes: 6 additions & 0 deletions tests/cases/fourslash/completionsTupleUnion2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/// <reference path="fourslash.ts" />

////declare const a: "a";
////declare const x: [{ t: "a" }, "b"] | [{ t: "b" }, "c"] = [{ t: a }, "/**/"]

verify.completions({ marker: "", exact: ["b"] });