Title: Type simultaneously does and does not have property · Issue #30657 · microsoft/TypeScript · GitHub
Open Graph Title: Type simultaneously does and does not have property · Issue #30657 · microsoft/TypeScript
X Title: Type simultaneously does and does not have property · Issue #30657 · microsoft/TypeScript
Description: TypeScript Version: 3.4.1 and 3.2.2 Code interface TextChannel { id: string; type: 'text'; phoneNumber: string; } interface EmailChannel { id: string; type: 'email'; addres: string; } type Channel = TextChannel | EmailChannel; export typ...
Open Graph Description: TypeScript Version: 3.4.1 and 3.2.2 Code interface TextChannel { id: string; type: 'text'; phoneNumber: string; } interface EmailChannel { id: string; type: 'email'; addres: string; } type Channel ...
X Description: TypeScript Version: 3.4.1 and 3.2.2 Code interface TextChannel { id: string; type: 'text'; phoneNumber: string; } interface EmailChannel { id: string; type: 'email'; addres: string;...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/30657
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Type simultaneously does and does not have property","articleBody":"**TypeScript Version:** 3.4.1 and 3.2.2\r\n\r\n**Code**\r\n\r\n```ts\r\ninterface TextChannel {\r\n id: string;\r\n type: 'text';\r\n phoneNumber: string;\r\n}\r\n\r\ninterface EmailChannel {\r\n id: string;\r\n type: 'email';\r\n addres: string;\r\n}\r\n\r\ntype Channel = TextChannel | EmailChannel;\r\n\r\nexport type ChannelType = Channel extends { type: infer R } ? R : never; // 'text' | 'email'\r\n\r\n// Stolen from lodash.\r\ntype Omit\u003cT, K extends keyof T> = Pick\u003c\r\n T,\r\n ({ [P in keyof T]: P } & { [P in K]: never } & { [x: string]: never })[keyof T]\r\n>;\r\n\r\ntype ChannelOfType\u003cT extends ChannelType, A = Channel> = A extends { type: T }\r\n ? A\r\n : never;\r\n\r\n/**\r\n * A NewChannel is a Channel with a 'type', a 'localChannelId', with no id,\r\n * and every other field is optional.\r\n */\r\nexport type NewChannel\u003cT extends Channel> = Pick\u003cT, 'type'> &\r\n Partial\u003cOmit\u003cT, 'type' | 'id'>> & { localChannelId: string };\r\n\r\n/**\r\n * Create a NewChannel of the specified type, ready to hand off to, for example,\r\n * a react form which can fill it in with values and turn it into a real Channel.\r\n */\r\nexport function makeNewChannel\u003cT extends ChannelType>(type: T): NewChannel\u003cChannelOfType\u003cT>> {\r\n const localChannelId = `blahblahblah`;\r\n return { type, localChannelId };\r\n}\r\n\r\n// Here's the exciting bit:\r\n\r\nconst newTextChannel = makeNewChannel('text');\r\n// Property 'phoneNumber' does not exist on type 'NewChannel\u003cTextChannel>'. ts(2339)\r\nnewTextChannel.phoneNumber = '613-555-1234';\r\n\r\nconst newTextChannel2 : NewChannel\u003cTextChannel> = makeNewChannel('text');\r\n// But this works!\r\nnewTextChannel2.phoneNumber = '613-555-1234';\r\n```\r\n\r\n**Expected behavior:**\r\n\r\nSince `newTextChannel` and `newTextChannel2` are both of type `NewChannel\u003cTextChannel>`, I'd expect them both to have a `phoneNumber` property (or both to not have a `phoneNumber` property).\r\n\r\n**Actual behavior:**\r\n\r\n`newTextChannel` does not have this property, and `newTextChannel2` does, even though these two seem to be of the same type!\r\n\r\n**Playground Link:** [link](https://www.typescriptlang.org/play/#src=interface%20TextChannel%20%7B%0D%0A%20%20%20%20id%3A%20string%3B%0D%0A%20%20%20%20type%3A%20'text'%3B%0D%0A%20%20%20%20phoneNumber%3A%20string%3B%0D%0A%7D%0D%0A%0D%0Ainterface%20EmailChannel%20%7B%0D%0A%20%20%20%20id%3A%20string%3B%0D%0A%20%20%20%20type%3A%20'email'%3B%0D%0A%20%20%20%20addres%3A%20string%3B%0D%0A%7D%0D%0A%0D%0Atype%20Channel%20%3D%20TextChannel%20%7C%20EmailChannel%3B%0D%0A%0D%0Aexport%20type%20ChannelType%20%3D%20Channel%20extends%20%7B%20type%3A%20infer%20R%20%7D%20%3F%20R%20%3A%20never%3B%20%2F%2F%20'text'%20%7C%20'email'%0D%0A%0D%0A%2F%2F%20Stolen%20from%20lodash.%0D%0Atype%20Omit%3CT%2C%20K%20extends%20keyof%20T%3E%20%3D%20Pick%3C%0D%0A%20%20%20%20T%2C%0D%0A%20%20%20%20(%7B%20%5BP%20in%20keyof%20T%5D%3A%20P%20%7D%20%26%20%7B%20%5BP%20in%20K%5D%3A%20never%20%7D%20%26%20%7B%20%5Bx%3A%20string%5D%3A%20never%20%7D)%5Bkeyof%20T%5D%0D%0A%3E%3B%0D%0A%0D%0Atype%20ChannelOfType%3CT%20extends%20ChannelType%2C%20A%20%3D%20Channel%3E%20%3D%20A%20extends%20%7B%20type%3A%20T%20%7D%0D%0A%20%20%20%20%3F%20A%0D%0A%20%20%20%20%3A%20never%3B%0D%0A%0D%0A%2F**%0D%0A%20*%20A%20NewChannel%20is%20a%20Channel%20with%20a%20'type'%2C%20a%20'localChannelId'%2C%20with%20no%20id%2C%0D%0A%20*%20and%20every%20other%20field%20is%20optional.%0D%0A%20*%2F%0D%0Aexport%20type%20NewChannel%3CT%20extends%20Channel%3E%20%3D%20Pick%3CT%2C%20'type'%3E%20%26%0D%0A%20%20%20%20Partial%3COmit%3CT%2C%20'type'%20%7C%20'id'%3E%3E%20%26%20%7B%20localChannelId%3A%20string%20%7D%3B%0D%0A%0D%0A%2F**%0D%0A%20*%20Create%20a%20NewChannel%20of%20the%20specified%20type%2C%20ready%20to%20hand%20off%20to%2C%20for%20example%2C%0D%0A%20*%20a%20react%20form%20which%20can%20fill%20it%20in%20with%20values%20and%20turn%20it%20into%20a%20real%20Channel.%0D%0A%20*%2F%0D%0Aexport%20function%20makeNewChannel%3CT%20extends%20ChannelType%3E(type%3A%20T)%3A%20NewChannel%3CChannelOfType%3CT%3E%3E%20%7B%0D%0A%20%20%20%20const%20localChannelId%20%3D%20%60blahblahblah%60%3B%0D%0A%20%20%20%20return%20%7B%20type%2C%20localChannelId%20%7D%3B%0D%0A%7D%0D%0A%0D%0A%2F%2F%20Here's%20the%20exciting%20bit%3A%0D%0A%0D%0Aconst%20newTextChannel%20%3D%20makeNewChannel('text')%3B%0D%0A%2F%2F%20Property%20'phoneNumber'%20does%20not%20exist%20on%20type%20'NewChannel%3CTextChannel%3E'.ts(2339)%0D%0AnewTextChannel.phoneNumber%20%3D%20'613-555-1234'%3B%0D%0A%0D%0Aconst%20newTextChannel2%20%3A%20NewChannel%3CTextChannel%3E%20%3D%20makeNewChannel('text')%3B%0D%0A%2F%2F%20But%20this%20works!%0D%0AnewTextChannel2.phoneNumber%20%3D%20'613-555-1234'%3B%0D%0A)\r\n\r\n**Related Issues:** No\r\n","author":{"url":"https://github.com/jwalton","@type":"Person","name":"jwalton"},"datePublished":"2019-03-30T03:09:21Z","url":"https://github.com/microsoft/TypeScript/issues/30657"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:8b6e1d6e-fc18-9e66-5331-a54b7f40b92b |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | CE9A:3D4705:B2CDFF:E76ED5:6B00E79E |
| html-safe-nonce | ee3586f90046bb63d8b640d6e243ac7b2568bbcede4a45bb3f19920fdf0ca70d |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDRTlBOjNENDcwNTpCMkNERkY6RTc2RUQ1OjZCMDBFNzlFIiwidmlzaXRvcl9pZCI6IjQ0MzQwODA3NTU5MzY3MTY3MDIiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 16ae0901897db797f38a7cea24746137d5caace2e8789060eecd81adef533368 |
| hovercard-subject-tag | issue:427235031 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/microsoft/TypeScript/30657/issue_layout |
| twitter:image | https://opengraph.githubassets.com/7c219e3a4197a5e079a6f21abf5e61ccaa4c4aaef8999efb6df60c7c89e73286/microsoft/TypeScript/issues/30657 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/7c219e3a4197a5e079a6f21abf5e61ccaa4c4aaef8999efb6df60c7c89e73286/microsoft/TypeScript/issues/30657 |
| og:image:alt | TypeScript Version: 3.4.1 and 3.2.2 Code interface TextChannel { id: string; type: 'text'; phoneNumber: string; } interface EmailChannel { id: string; type: 'email'; addres: string; } type Channel ... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | jwalton |
| hostname | github.com |
| expected-hostname | github.com |
| None | 27088b6977484d0d3321d9a11c907c6d6637b315cfa359ef7952dbf6292f9b14 |
| turbo-cache-control | no-preview |
| go-import | github.com/microsoft/TypeScript git https://github.com/microsoft/TypeScript.git |
| octolytics-dimension-user_id | 6154722 |
| octolytics-dimension-user_login | microsoft |
| octolytics-dimension-repository_id | 20929025 |
| octolytics-dimension-repository_nwo | microsoft/TypeScript |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 20929025 |
| octolytics-dimension-repository_network_root_nwo | microsoft/TypeScript |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 624179fb8b88d26ba5bba08894eb60a1c5a1d93d |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width