Handle shorthand property assignments

This commit is contained in:
Anders Hejlsberg 2016-04-08 15:08:22 -07:00
parent 019f5bd4e8
commit f13c92f036
2 changed files with 12 additions and 3 deletions

View file

@ -7358,7 +7358,7 @@ namespace ts {
return createArrayType(elementType);
}
function getAssignedTypeOfPropertyAssignment(node: PropertyAssignment): Type {
function getAssignedTypeOfPropertyAssignment(node: PropertyAssignment | ShorthandPropertyAssignment): Type {
const objectType = getAssignedType(<ObjectLiteralExpression>node.parent);
const text = getTextOfPropertyName(node.name);
return getTypeOfPropertyOfType(objectType, text) ||
@ -7367,6 +7367,15 @@ namespace ts {
unknownType;
}
function getAssignedTypeOfShorthandPropertyAssignment(node: ShorthandPropertyAssignment): Type {
if (node.objectAssignmentInitializer) {
const defaultType = checkExpressionCached(node.objectAssignmentInitializer);
const assignedType = getAssignedTypeOfPropertyAssignment(node);
return getUnionType([getTypeWithFacts(assignedType, TypeFacts.NEUndefined), defaultType]);
}
return getAssignedTypeOfPropertyAssignment(node);
}
function getAssignedType(node: Expression): Type {
const parent = node.parent;
switch (parent.kind) {
@ -7383,7 +7392,7 @@ namespace ts {
case SyntaxKind.PropertyAssignment:
return getAssignedTypeOfPropertyAssignment(<PropertyAssignment>parent);
case SyntaxKind.ShorthandPropertyAssignment:
break; // !!! TODO
return getAssignedTypeOfShorthandPropertyAssignment(<ShorthandPropertyAssignment>parent);
}
return unknownType;
}

View file

@ -1329,7 +1329,7 @@ namespace ts {
node = parent;
continue;
}
if (parent.kind === SyntaxKind.PropertyAssignment) {
if (parent.kind === SyntaxKind.PropertyAssignment || parent.kind === SyntaxKind.ShorthandPropertyAssignment) {
node = parent.parent;
continue;
}