From 883385f5cb321fc8008e5d4f3727412fee41d0dc Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Thu, 22 Jan 2015 11:03:15 -0800 Subject: [PATCH] Include missing optional properties in contextually typed object literal --- src/compiler/checker.ts | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 1e84c78fce1..a3ea14dcb09 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -5455,6 +5455,16 @@ module ts { } } + // If object literal is contextually typed, add missing optional properties from the contextual type + // such that the resulting type becomes a subtype in cases where only optional properties were omitted + if (contextualType) { + forEach(getPropertiesOfObjectType(contextualType), p => { + if (p.flags & SymbolFlags.Optional && !hasProperty(properties, p.name)) { + properties[p.name] = p; + } + }); + } + var stringIndexType = getIndexType(IndexKind.String); var numberIndexType = getIndexType(IndexKind.Number); var result = createAnonymousType(node.symbol, properties, emptyArray, emptyArray, stringIndexType, numberIndexType);