{"action":"created","issue":{"url":"https://api.github.com/repos/Microsoft/TypeScript/issues/8545","repository_url":"https://api.github.com/repos/Microsoft/TypeScript","labels_url":"https://api.github.com/repos/Microsoft/TypeScript/issues/8545/labels{/name}","comments_url":"https://api.github.com/repos/Microsoft/TypeScript/issues/8545/comments","events_url":"https://api.github.com/repos/Microsoft/TypeScript/issues/8545/events","html_url":"https://github.com/Microsoft/TypeScript/issues/8545","id":154037156,"number":8545,"title":"add object initializers","user":{"login":"aleksey-bykov","id":937933,"avatar_url":"https://avatars.githubusercontent.com/u/937933?v=3","gravatar_id":"","url":"https://api.github.com/users/aleksey-bykov","html_url":"https://github.com/aleksey-bykov","followers_url":"https://api.github.com/users/aleksey-bykov/followers","following_url":"https://api.github.com/users/aleksey-bykov/following{/other_user}","gists_url":"https://api.github.com/users/aleksey-bykov/gists{/gist_id}","starred_url":"https://api.github.com/users/aleksey-bykov/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/aleksey-bykov/subscriptions","organizations_url":"https://api.github.com/users/aleksey-bykov/orgs","repos_url":"https://api.github.com/users/aleksey-bykov/repos","events_url":"https://api.github.com/users/aleksey-bykov/events{/privacy}","received_events_url":"https://api.github.com/users/aleksey-bykov/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":1,"created_at":"2016-05-10T15:33:00Z","updated_at":"2016-05-10T17:52:49Z","closed_at":null,"body":"Currently there are 2 ways to enforce implementing an interface over an object:\r\n\r\n- creating a object literal in an interface type context\r\n- constructing an instance of a class that implements the said interface\r\n\r\nIn both cases we deal with a newly created object, however there are also situations when an already created object needs to be extended to comply to some more elaborate interface.\r\n\r\nA good example are mixins and scenarios alike:\r\n\r\n```typescript\r\ninterface DoThis {\r\n   x: any;\r\n   y: any;\r\n}\r\ninterface DoThat {\r\n   z: any\r\n}\r\ninterface DoBoth implements DoThis, DoThat {\r\n}\r\n\r\nfunction extend(doThis: DoThis): DoBoth {\r\n    // need to make sure that all properties of DoThat are set\r\n    // wish could do just\r\n    //       doThis.z = undefined;\r\n    //       return doThis;\r\n    // have to do\r\n    return {\r\n        x: doThis.x,\r\n        y: doThis.y\r\n        z: undefined\r\n    };\r\n}\r\n\r\nlet doThis : DoThis = { x: undefined, y: undefined };\r\nlet doBoth = extend(doThis);\r\n```\r\n\r\nI am not aware how to make the extend function type safe, other than copying each field from `doThis` to the resulting object. Which might or might not be a solution (functions are harder to extend this way). If only TypeScript had a way to enforce an interface on the given object it would be very helpful.\r\n"},"comment":{"url":"https://api.github.com/repos/Microsoft/TypeScript/issues/comments/218237352","html_url":"https://github.com/Microsoft/TypeScript/issues/8545#issuecomment-218237352","issue_url":"https://api.github.com/repos/Microsoft/TypeScript/issues/8545","id":218237352,"user":{"login":"mhegazy","id":8000722,"avatar_url":"https://avatars.githubusercontent.com/u/8000722?v=3","gravatar_id":"","url":"https://api.github.com/users/mhegazy","html_url":"https://github.com/mhegazy","followers_url":"https://api.github.com/users/mhegazy/followers","following_url":"https://api.github.com/users/mhegazy/following{/other_user}","gists_url":"https://api.github.com/users/mhegazy/gists{/gist_id}","starred_url":"https://api.github.com/users/mhegazy/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mhegazy/subscriptions","organizations_url":"https://api.github.com/users/mhegazy/orgs","repos_url":"https://api.github.com/users/mhegazy/repos","events_url":"https://api.github.com/users/mhegazy/events{/privacy}","received_events_url":"https://api.github.com/users/mhegazy/received_events","type":"User","site_admin":false},"created_at":"2016-05-10T17:52:49Z","updated_at":"2016-05-10T17:52:49Z","body":"type assertion?\r\n\r\n```ts\r\nfunction extend(doThis: DoThis): DoBoth {\r\n    return doThis as DoBoth;\r\n}\r\n```"}}