From f13c92f0366acb29c01917bd1ddc18bedf428fda Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Fri, 8 Apr 2016 15:08:22 -0700 Subject: [PATCH] Handle shorthand property assignments --- src/compiler/checker.ts | 13 +++++++++++-- src/compiler/utilities.ts | 2 +- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 2393b236648..918acb410aa 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -7358,7 +7358,7 @@ namespace ts { return createArrayType(elementType); } - function getAssignedTypeOfPropertyAssignment(node: PropertyAssignment): Type { + function getAssignedTypeOfPropertyAssignment(node: PropertyAssignment | ShorthandPropertyAssignment): Type { const objectType = getAssignedType(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(parent); case SyntaxKind.ShorthandPropertyAssignment: - break; // !!! TODO + return getAssignedTypeOfShorthandPropertyAssignment(parent); } return unknownType; } diff --git a/src/compiler/utilities.ts b/src/compiler/utilities.ts index 45567538dcd..f6a4b81e6d2 100644 --- a/src/compiler/utilities.ts +++ b/src/compiler/utilities.ts @@ -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; }