{"action":"created","issue":{"url":"https://api.github.com/repos/Microsoft/TypeScript/issues/22294","repository_url":"https://api.github.com/repos/Microsoft/TypeScript","labels_url":"https://api.github.com/repos/Microsoft/TypeScript/issues/22294/labels{/name}","comments_url":"https://api.github.com/repos/Microsoft/TypeScript/issues/22294/comments","events_url":"https://api.github.com/repos/Microsoft/TypeScript/issues/22294/events","html_url":"https://github.com/Microsoft/TypeScript/issues/22294","id":301807180,"number":22294,"title":"Assert union types using property checking","user":{"login":"vinz243","id":4569266,"avatar_url":"https://avatars0.githubusercontent.com/u/4569266?v=4","gravatar_id":"","url":"https://api.github.com/users/vinz243","html_url":"https://github.com/vinz243","followers_url":"https://api.github.com/users/vinz243/followers","following_url":"https://api.github.com/users/vinz243/following{/other_user}","gists_url":"https://api.github.com/users/vinz243/gists{/gist_id}","starred_url":"https://api.github.com/users/vinz243/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/vinz243/subscriptions","organizations_url":"https://api.github.com/users/vinz243/orgs","repos_url":"https://api.github.com/users/vinz243/repos","events_url":"https://api.github.com/users/vinz243/events{/privacy}","received_events_url":"https://api.github.com/users/vinz243/received_events","type":"User","site_admin":false},"labels":[{"id":113971558,"url":"https://api.github.com/repos/Microsoft/TypeScript/labels/Question","name":"Question","color":"d4c5f9","default":false}],"state":"closed","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":1,"created_at":"2018-03-02T15:37:55Z","updated_at":"2018-03-02T16:08:33Z","closed_at":"2018-03-02T16:08:33Z","author_association":"NONE","body":"**TypeScript Version:**  2.8.0-dev.20180302\r\n\r\n**Search Terms:** Typescript type assert from properties checking\r\n\r\n**Code**\r\n\r\n```ts\r\nexport type FooOrBar = { foo: string } | { bar: string, foobar: string};\r\n\r\nlet stack: FooOrBar;\r\n\r\nif (stack.bar) {\r\n    const { bar, foobar } = stack;\r\n    // ...\r\n}\r\n```\r\n\r\n**Expected behavior:**\r\n\r\nTypescript should determine that since we are checking if a property is present on an Union type, it can then cast it to the specific sub type \r\n```ts\r\n{ \r\n  bar: string,\r\n  foobar: string\r\n}\r\n```\r\n\r\n**Actual behavior:**\r\n\r\n```\r\nexport type FooOrBar = { foo: string } || { bar: string, foobar: string};\r\n\r\nlet stack: FooOrBar;\r\n\r\nif (stack.bar) {\r\n          ^^^\r\n\r\n        Property 'bar' does not exist on type 'FooOrBar'.\r\n\r\n        const { bar, foobar } = stack;\r\n                ^^^  ^^^^^^\r\n\r\nType 'FooOrBar' has no property 'bar' and no string index signature.\r\nType 'FooOrBar' has no property 'foobar' and no string index signature.\r\n\r\n}\r\n```\r\n\r\n\r\n**Playground Link:** [Here](https://www.typescriptlang.org/play/index.html#src=export%20type%20FooOrBar%20%3D%20%7B%20foo%3A%20string%20%7D%20%7C%20%7B%20bar%3A%20string%2C%20foobar%3A%20string%7D%3B%0D%0A%0D%0Alet%20stack%3A%20FooOrBar%3B%0D%0A%0D%0Aif%20(stack.bar)%20%7B%0D%0A%20%20%20%20const%20%7B%20bar%2C%20foobar%20%7D%20%3D%20stack%3B%0D%0A%20%20%20%20%2F%2F%20...%0D%0A%7D)\r\n**Related Issues:**\r\n\r\nI am aware you can cast it using `<>` or `as`, but it would be really convenient to guess, because this requires to name every possible type, which we all know takes a really long time per variable.\r\n"},"comment":{"url":"https://api.github.com/repos/Microsoft/TypeScript/issues/comments/369966585","html_url":"https://github.com/Microsoft/TypeScript/issues/22294#issuecomment-369966585","issue_url":"https://api.github.com/repos/Microsoft/TypeScript/issues/22294","id":369966585,"user":{"login":"vinz243","id":4569266,"avatar_url":"https://avatars0.githubusercontent.com/u/4569266?v=4","gravatar_id":"","url":"https://api.github.com/users/vinz243","html_url":"https://github.com/vinz243","followers_url":"https://api.github.com/users/vinz243/followers","following_url":"https://api.github.com/users/vinz243/following{/other_user}","gists_url":"https://api.github.com/users/vinz243/gists{/gist_id}","starred_url":"https://api.github.com/users/vinz243/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/vinz243/subscriptions","organizations_url":"https://api.github.com/users/vinz243/orgs","repos_url":"https://api.github.com/users/vinz243/repos","events_url":"https://api.github.com/users/vinz243/events{/privacy}","received_events_url":"https://api.github.com/users/vinz243/received_events","type":"User","site_admin":false},"created_at":"2018-03-02T16:08:33Z","updated_at":"2018-03-02T16:08:33Z","author_association":"NONE","body":"Awesome, thanks!"}}
{"actor":{"display_login":"vinz243"}}