Check assignability: rest destructuring assignment

This commit is contained in:
Nathan Shively-Sanders 2016-11-16 13:39:00 -08:00
parent 641948fb0c
commit 074014e039

View file

@ -14188,12 +14188,13 @@ namespace ts {
function checkObjectLiteralAssignment(node: ObjectLiteralExpression, sourceType: Type): Type {
const properties = node.properties;
for (const p of properties) {
checkObjectLiteralDestructuringPropertyAssignment(sourceType, p);
checkObjectLiteralDestructuringPropertyAssignment(sourceType, p, properties);
}
return sourceType;
}
function checkObjectLiteralDestructuringPropertyAssignment(objectLiteralType: Type, property: ObjectLiteralElementLike) {
/** Note: If property cannot be a SpreadAssignment, then allProperties does not need to be provided */
function checkObjectLiteralDestructuringPropertyAssignment(objectLiteralType: Type, property: ObjectLiteralElementLike, allProperties?: ObjectLiteralElementLike[]) {
if (property.kind === SyntaxKind.PropertyAssignment || property.kind === SyntaxKind.ShorthandPropertyAssignment) {
const name = <PropertyName>(<PropertyAssignment>property).name;
if (name.kind === SyntaxKind.ComputedPropertyName) {
@ -14223,7 +14224,14 @@ namespace ts {
}
}
else if (property.kind === SyntaxKind.SpreadAssignment) {
checkReferenceExpression(property.expression, Diagnostics.The_target_of_an_object_rest_assignment_must_be_a_variable_or_a_property_access);
const nonRestNames: PropertyName[] = [];
if (allProperties) {
for (let i = 0; i < allProperties.length - 1; i++) {
nonRestNames.push(allProperties[i].name);
}
}
const type = getRestType(objectLiteralType, nonRestNames, objectLiteralType.symbol);
return checkDestructuringAssignment(property.expression, type);
}
else {
error(property, Diagnostics.Property_assignment_expected);
@ -14321,7 +14329,10 @@ namespace ts {
function checkReferenceAssignment(target: Expression, sourceType: Type, contextualMapper?: TypeMapper): Type {
const targetType = checkExpression(target, contextualMapper);
if (checkReferenceExpression(target, Diagnostics.The_left_hand_side_of_an_assignment_expression_must_be_a_variable_or_a_property_access)) {
const error = target.parent.kind === SyntaxKind.SpreadAssignment ?
Diagnostics.The_target_of_an_object_rest_assignment_must_be_a_variable_or_a_property_access :
Diagnostics.The_left_hand_side_of_an_assignment_expression_must_be_a_variable_or_a_property_access;
if (checkReferenceExpression(target, error)) {
checkTypeAssignableTo(sourceType, targetType, target, /*headMessage*/ undefined);
}
return sourceType;