Skip to content

Commit b857439

Browse files
committed
Check assignability: rest destructuring assignment
1 parent 6b0de7b commit b857439

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
@@ -14265,12 +14265,13 @@ namespace ts {
1426514265
function checkObjectLiteralAssignment(node: ObjectLiteralExpression, sourceType: Type): Type {
1426614266
const properties = node.properties;
1426714267
for (const p of properties) {
14268-
checkObjectLiteralDestructuringPropertyAssignment(sourceType, p);
14268+
checkObjectLiteralDestructuringPropertyAssignment(sourceType, p, properties);
1426914269
}
1427014270
return sourceType;
1427114271
}
1427214272

14273-
function checkObjectLiteralDestructuringPropertyAssignment(objectLiteralType: Type, property: ObjectLiteralElementLike) {
14273+
/** Note: If property cannot be a SpreadAssignment, then allProperties does not need to be provided */
14274+
function checkObjectLiteralDestructuringPropertyAssignment(objectLiteralType: Type, property: ObjectLiteralElementLike, allProperties?: ObjectLiteralElementLike[]) {
1427414275
if (property.kind === SyntaxKind.PropertyAssignment || property.kind === SyntaxKind.ShorthandPropertyAssignment) {
1427514276
const name = <PropertyName>(<PropertyAssignment>property).name;
1427614277
if (name.kind === SyntaxKind.ComputedPropertyName) {
@@ -14300,7 +14301,14 @@ namespace ts {
1430014301
}
1430114302
}
1430214303
else if (property.kind === SyntaxKind.SpreadAssignment) {
14303-
checkReferenceExpression(property.expression, Diagnostics.The_target_of_an_object_rest_assignment_must_be_a_variable_or_a_property_access);
14304+
const nonRestNames: PropertyName[] = [];
14305+
if (allProperties) {
14306+
for (let i = 0; i < allProperties.length - 1; i++) {
14307+
nonRestNames.push(allProperties[i].name);
14308+
}
14309+
}
14310+
const type = getRestType(objectLiteralType, nonRestNames, objectLiteralType.symbol);
14311+
return checkDestructuringAssignment(property.expression, type);
1430414312
}
1430514313
else {
1430614314
error(property, Diagnostics.Property_assignment_expected);
@@ -14398,7 +14406,10 @@ namespace ts {
1439814406

1439914407
function checkReferenceAssignment(target: Expression, sourceType: Type, contextualMapper?: TypeMapper): Type {
1440014408
const targetType = checkExpression(target, contextualMapper);
14401-
if (checkReferenceExpression(target, Diagnostics.The_left_hand_side_of_an_assignment_expression_must_be_a_variable_or_a_property_access)) {
14409+
const error = target.parent.kind === SyntaxKind.SpreadAssignment ?
14410+
Diagnostics.The_target_of_an_object_rest_assignment_must_be_a_variable_or_a_property_access :
14411+
Diagnostics.The_left_hand_side_of_an_assignment_expression_must_be_a_variable_or_a_property_access;
14412+
if (checkReferenceExpression(target, error)) {
1440214413
checkTypeAssignableTo(sourceType, targetType, target, /*headMessage*/ undefined);
1440314414
}
1440414415
return sourceType;

0 commit comments

Comments
 (0)