{"action":"created","issue":{"url":"https://api.github.com/repos/Microsoft/TypeScript/issues/1774","labels_url":"https://api.github.com/repos/Microsoft/TypeScript/issues/1774/labels{/name}","comments_url":"https://api.github.com/repos/Microsoft/TypeScript/issues/1774/comments","events_url":"https://api.github.com/repos/Microsoft/TypeScript/issues/1774/events","html_url":"https://github.com/Microsoft/TypeScript/pull/1774","id":55199190,"number":1774,"title":"Include missing optional properties in contextually typed object literals","user":{"login":"ahejlsberg","id":4226954,"avatar_url":"https://avatars.githubusercontent.com/u/4226954?v=3","gravatar_id":"","url":"https://api.github.com/users/ahejlsberg","html_url":"https://github.com/ahejlsberg","followers_url":"https://api.github.com/users/ahejlsberg/followers","following_url":"https://api.github.com/users/ahejlsberg/following{/other_user}","gists_url":"https://api.github.com/users/ahejlsberg/gists{/gist_id}","starred_url":"https://api.github.com/users/ahejlsberg/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ahejlsberg/subscriptions","organizations_url":"https://api.github.com/users/ahejlsberg/orgs","repos_url":"https://api.github.com/users/ahejlsberg/repos","events_url":"https://api.github.com/users/ahejlsberg/events{/privacy}","received_events_url":"https://api.github.com/users/ahejlsberg/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/Microsoft/TypeScript/labels/cla-not-required","name":"cla-not-required","color":"009800"}],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":5,"created_at":"2015-01-22T19:43:52Z","updated_at":"2015-01-22T23:16:04Z","closed_at":null,"pull_request":{"url":"https://api.github.com/repos/Microsoft/TypeScript/pulls/1774","html_url":"https://github.com/Microsoft/TypeScript/pull/1774","diff_url":"https://github.com/Microsoft/TypeScript/pull/1774.diff","patch_url":"https://github.com/Microsoft/TypeScript/pull/1774.patch"},"body":"Fixes #1723.\r\n\r\nIn #919 we tightened the subtyping rules to require optional properties to be present in subtypes. That's still the right thing to do, but it can have unintended consequences for overload resolution (which relies on subtyping) in combination with object literals.\r\n\r\nWith this PR, when an object literal is contextually typed by a type T, any optional properties in T that are omitted in the object literal are included in the resulting type, thus ensuring the resulting type is a subtype of T when all that was omitted was optional properties (whew!). For example\r\n```typescript\r\ninterface Item {\r\n    name: string;\r\n    description?: string;\r\n}\r\n\r\ndeclare function foo(item: Item): string;\r\ndeclare function foo(item: any): number;\r\n\r\nvar x = foo({ name: \"Sprocket\" });  // x of type string\r\nvar y = foo({ name: \"Sprocket\", description: \"Bumpy wheel\" });  // y of type string\r\nvar z = foo({ a: 10 });  // z of type number\r\n```\r\nIn the first call to foo above, contextual typing of the object literal adds the missing optional property. Thus, the argument expression becomes a subtype of Item, and the first overload is chosen (as one would expect).\r\n\r\nThis change does at times affect error reporting. For example\r\n```typescript\r\nvar f: Item = { naem: \"Hello\" };\r\n```\r\nHere we'll now report\r\n```\r\nt.ts(11,5): error TS2322: Type '{ naem: string; description?: string; }' is not assignable to type 'Item'.\r\n  Property 'name' is missing in type '{ naem: string; description?: string; }'.\r\n```\r\nDepending on your point of view it may be surprising that the reported type includes an optional property that you didn't actually write (but which is there because of your context)."},"comment":{"url":"https://api.github.com/repos/Microsoft/TypeScript/issues/comments/71119246","html_url":"https://github.com/Microsoft/TypeScript/pull/1774#issuecomment-71119246","issue_url":"https://api.github.com/repos/Microsoft/TypeScript/issues/1774","id":71119246,"user":{"login":"JsonFreeman","id":8052525,"avatar_url":"https://avatars.githubusercontent.com/u/8052525?v=3","gravatar_id":"","url":"https://api.github.com/users/JsonFreeman","html_url":"https://github.com/JsonFreeman","followers_url":"https://api.github.com/users/JsonFreeman/followers","following_url":"https://api.github.com/users/JsonFreeman/following{/other_user}","gists_url":"https://api.github.com/users/JsonFreeman/gists{/gist_id}","starred_url":"https://api.github.com/users/JsonFreeman/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/JsonFreeman/subscriptions","organizations_url":"https://api.github.com/users/JsonFreeman/orgs","repos_url":"https://api.github.com/users/JsonFreeman/repos","events_url":"https://api.github.com/users/JsonFreeman/events{/privacy}","received_events_url":"https://api.github.com/users/JsonFreeman/received_events","type":"User","site_admin":false},"created_at":"2015-01-22T23:16:04Z","updated_at":"2015-01-22T23:16:04Z","body":":+1: "}}