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);