Skip to content

Commit 23c5f92

Browse files
authored
fix(3758): show completion for object literals in an assignment pattern (#40976)
1 parent 197ac80 commit 23c5f92

File tree

3 files changed

+31
-4
lines changed

3 files changed

+31
-4
lines changed

src/services/completions.ts

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1891,10 +1891,12 @@ namespace ts.Completions {
18911891
let existingMembers: readonly Declaration[] | undefined;
18921892

18931893
if (objectLikeContainer.kind === SyntaxKind.ObjectLiteralExpression) {
1894-
const instantiatedType = typeChecker.getContextualType(objectLikeContainer);
1895-
const completionsType = instantiatedType && typeChecker.getContextualType(objectLikeContainer, ContextFlags.Completions);
1896-
if (!instantiatedType || !completionsType) return GlobalsSearch.Fail;
1897-
isNewIdentifierLocation = hasIndexSignature(instantiatedType || completionsType);
1894+
const instantiatedType = tryGetObjectLiteralContextualType(objectLikeContainer, typeChecker);
1895+
if (instantiatedType === undefined) {
1896+
return GlobalsSearch.Fail;
1897+
}
1898+
const completionsType = typeChecker.getContextualType(objectLikeContainer, ContextFlags.Completions);
1899+
isNewIdentifierLocation = hasIndexSignature(completionsType || instantiatedType);
18981900
typeMembers = getPropertiesForObjectExpression(instantiatedType, completionsType, objectLikeContainer, typeChecker);
18991901
existingMembers = objectLikeContainer.properties;
19001902
}
@@ -2830,4 +2832,15 @@ namespace ts.Completions {
28302832
function isStaticProperty(symbol: Symbol) {
28312833
return !!(symbol.valueDeclaration && getEffectiveModifierFlags(symbol.valueDeclaration) & ModifierFlags.Static && isClassLike(symbol.valueDeclaration.parent));
28322834
}
2835+
2836+
function tryGetObjectLiteralContextualType(node: ObjectLiteralExpression, typeChecker: TypeChecker) {
2837+
const type = typeChecker.getContextualType(node);
2838+
if (type) {
2839+
return type;
2840+
}
2841+
if (isBinaryExpression(node.parent) && node.parent.operatorToken.kind === SyntaxKind.EqualsToken) {
2842+
return typeChecker.getTypeAtLocation(node.parent);
2843+
}
2844+
return undefined;
2845+
}
28332846
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
2+
/// <reference path="fourslash.ts" />
3+
4+
////let x = { a: 1, b: 2 };
5+
////let y = ({ /**/ } = x, 1);
6+
7+
verify.completions({ marker: "", exact: ["a", "b"] });
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
2+
/// <reference path="fourslash.ts" />
3+
4+
////let x = { a: 1, b: 2 };
5+
////let y = ({ a, /**/ } = x, 1);
6+
7+
verify.completions({ marker: "", exact: ["b"] });

0 commit comments

Comments
 (0)