diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 97749c780b0e2..c346dc97f76cf 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -83,7 +83,7 @@ namespace ts { getShorthandAssignmentValueSymbol, getExportSpecifierLocalTargetSymbol, getTypeAtLocation: getTypeOfNode, - getTypeOfArrayLiteralOrObjectLiteralDestructuringAssignment, + getPropertySymbolOfDestructuringAssignment, typeToString, getSymbolDisplayBuilder, symbolToString, @@ -16568,6 +16568,7 @@ namespace ts { // [ a ] from // [a] = [ some array ...] function getTypeOfArrayLiteralOrObjectLiteralDestructuringAssignment(expr: Expression): Type { + Debug.assert(expr.kind === SyntaxKind.ObjectLiteralExpression || expr.kind === SyntaxKind.ArrayLiteralExpression); // If this is from "for of" // for ( { a } of elems) { // } @@ -16596,6 +16597,18 @@ namespace ts { indexOf((expr.parent).elements, expr), elementType || unknownType); } + // Gets the property symbol corresponding to the property in destructuring assignment + // 'property1' from + // for ( { property1: a } of elems) { + // } + // 'property1' at location 'a' from: + // [a] = [ property1, property2 ] + function getPropertySymbolOfDestructuringAssignment(location: Identifier) { + // Get the type of the object or array literal and then look for property of given name in the type + const typeOfObjectLiteral = getTypeOfArrayLiteralOrObjectLiteralDestructuringAssignment(location.parent.parent); + return typeOfObjectLiteral && getPropertyOfType(typeOfObjectLiteral, location.text); + } + function getTypeOfExpression(expr: Expression): Type { if (isRightSideOfQualifiedNameOrPropertyAccess(expr)) { expr = expr.parent; diff --git a/src/compiler/types.ts b/src/compiler/types.ts index d5b9ab6d14326..17351babc7849 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -1735,7 +1735,7 @@ namespace ts { getSymbolsOfParameterPropertyDeclaration(parameter: ParameterDeclaration, parameterName: string): Symbol[]; getShorthandAssignmentValueSymbol(location: Node): Symbol; getExportSpecifierLocalTargetSymbol(location: ExportSpecifier): Symbol; - getTypeOfArrayLiteralOrObjectLiteralDestructuringAssignment(expression: Expression): Type; + getPropertySymbolOfDestructuringAssignment(location: Identifier): Symbol; getTypeAtLocation(node: Node): Type; typeToString(type: Type, enclosingDeclaration?: Node, flags?: TypeFormatFlags): string; symbolToString(symbol: Symbol, enclosingDeclaration?: Node, meaning?: SymbolFlags): string; diff --git a/src/services/services.ts b/src/services/services.ts index d79e2b94a74ec..a30bc47b83c50 100644 --- a/src/services/services.ts +++ b/src/services/services.ts @@ -5651,14 +5651,8 @@ namespace ts { } function getPropertySymbolOfDestructuringAssignment(location: Node) { - if (isArrayLiteralOrObjectLiteralDestructuringPattern(location.parent.parent)) { - // Do work to determine if this is a property symbol corresponding to the search symbol - const typeOfObjectLiteral = typeChecker.getTypeOfArrayLiteralOrObjectLiteralDestructuringAssignment(location.parent.parent); - if (typeOfObjectLiteral) { - return typeChecker.getPropertyOfType(typeOfObjectLiteral, (location).text); - } - } - return undefined; + return isArrayLiteralOrObjectLiteralDestructuringPattern(location.parent.parent) && + typeChecker.getPropertySymbolOfDestructuringAssignment(location); } function isObjectBindingPatternElementWithoutPropertyName(symbol: Symbol) {