Skip to content

Commit 074014e

Browse files
committed
Check assignability: rest destructuring assignment
1 parent 641948f commit 074014e

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

src/compiler/checker.ts

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14188,12 +14188,13 @@ namespace ts {
1418814188
function checkObjectLiteralAssignment(node: ObjectLiteralExpression, sourceType: Type): Type {
1418914189
const properties = node.properties;
1419014190
for (const p of properties) {
14191-
checkObjectLiteralDestructuringPropertyAssignment(sourceType, p);
14191+
checkObjectLiteralDestructuringPropertyAssignment(sourceType, p, properties);
1419214192
}
1419314193
return sourceType;
1419414194
}
1419514195

14196-
function checkObjectLiteralDestructuringPropertyAssignment(objectLiteralType: Type, property: ObjectLiteralElementLike) {
14196+
/** Note: If property cannot be a SpreadAssignment, then allProperties does not need to be provided */
14197+
function checkObjectLiteralDestructuringPropertyAssignment(objectLiteralType: Type, property: ObjectLiteralElementLike, allProperties?: ObjectLiteralElementLike[]) {
1419714198
if (property.kind === SyntaxKind.PropertyAssignment || property.kind === SyntaxKind.ShorthandPropertyAssignment) {
1419814199
const name = <PropertyName>(<PropertyAssignment>property).name;
1419914200
if (name.kind === SyntaxKind.ComputedPropertyName) {
@@ -14223,7 +14224,14 @@ namespace ts {
1422314224
}
1422414225
}
1422514226
else if (property.kind === SyntaxKind.SpreadAssignment) {
14226-
checkReferenceExpression(property.expression, Diagnostics.The_target_of_an_object_rest_assignment_must_be_a_variable_or_a_property_access);
14227+
const nonRestNames: PropertyName[] = [];
14228+
if (allProperties) {
14229+
for (let i = 0; i < allProperties.length - 1; i++) {
14230+
nonRestNames.push(allProperties[i].name);
14231+
}
14232+
}
14233+
const type = getRestType(objectLiteralType, nonRestNames, objectLiteralType.symbol);
14234+
return checkDestructuringAssignment(property.expression, type);
1422714235
}
1422814236
else {
1422914237
error(property, Diagnostics.Property_assignment_expected);
@@ -14321,7 +14329,10 @@ namespace ts {
1432114329

1432214330
function checkReferenceAssignment(target: Expression, sourceType: Type, contextualMapper?: TypeMapper): Type {
1432314331
const targetType = checkExpression(target, contextualMapper);
14324-
if (checkReferenceExpression(target, Diagnostics.The_left_hand_side_of_an_assignment_expression_must_be_a_variable_or_a_property_access)) {
14332+
const error = target.parent.kind === SyntaxKind.SpreadAssignment ?
14333+
Diagnostics.The_target_of_an_object_rest_assignment_must_be_a_variable_or_a_property_access :
14334+
Diagnostics.The_left_hand_side_of_an_assignment_expression_must_be_a_variable_or_a_property_access;
14335+
if (checkReferenceExpression(target, error)) {
1432514336
checkTypeAssignableTo(sourceType, targetType, target, /*headMessage*/ undefined);
1432614337
}
1432714338
return sourceType;

0 commit comments

Comments
 (0)