Title: Method returning index type breaks type interference of union types · Issue #30717 · microsoft/TypeScript · GitHub
Open Graph Title: Method returning index type breaks type interference of union types · Issue #30717 · microsoft/TypeScript
X Title: Method returning index type breaks type interference of union types · Issue #30717 · microsoft/TypeScript
Description: TypeScript Version: 3.4.0-dev.201xxxxx Search Terms: union, index type Code class Test
Open Graph Description: TypeScript Version: 3.4.0-dev.201xxxxx Search Terms: union, index type Code class Test
X Description: TypeScript Version: 3.4.0-dev.201xxxxx Search Terms: union, index type Code class Test<Props extends Object> { constructor(public o: Props) {} get<K extends keyof Props>(k: K) : Props[K...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/30717
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Method returning index type breaks type interference of union types","articleBody":"\u003c!-- 🚨 STOP 🚨 𝗦𝗧𝗢𝗣 🚨 𝑺𝑻𝑶𝑷 🚨\r\n\r\nHalf of all issues filed here are duplicates, answered in the FAQ, or not appropriate for the bug tracker. Even if you think you've found a *bug*, please read the FAQ first, especially the Common \"Bugs\" That Aren't Bugs section!\r\n\r\nPlease help us by doing the following steps before logging an issue:\r\n * Search: https://github.com/Microsoft/TypeScript/search?type=Issues\r\n * Read the FAQ: https://github.com/Microsoft/TypeScript/wiki/FAQ\r\n\r\nPlease fill in the *entire* template below.\r\n-->\r\n\r\n\u003c!-- Please try to reproduce the issue with `typescript@next`. It may have already been fixed. -->\r\n**TypeScript Version:** 3.4.0-dev.201xxxxx\r\n\r\n\u003c!-- Search terms you tried before logging this (so others can find this issue more easily) -->\r\n**Search Terms:** union, index type\r\n\r\n**Code**\r\n\r\n```ts\r\nclass Test\u003cProps extends Object> {\r\n constructor(public o: Props) {}\r\n get\u003cK extends keyof Props>(k: K) : Props[K] { return this.o[k]; }\r\n}\r\n\r\ninterface A {t: \"A\";};\r\ninterface B {t: \"B\";};\r\ntype u = Test\u003cA> | Test\u003cB>;\r\n\r\nconst tmp = new Test\u003cB>({t: 'B'}) as u;\r\n\r\nswitch(tmp.get('t')) {\r\n case 'A': break;\r\n case 'B': break;\r\n}\r\n```\r\n\r\n**Expected behavior:**\r\n\r\nCode compiles with `tmp.get('t')` having type `\"A\" | \"B\"`.\r\n\r\nThis works for:\r\n\r\n* [simple objects/interfaces](https://www.typescriptlang.org/play/#src=interface%20A%20%7B%20t%3A%20'A'%3B%20%7D%3B%0D%0Ainterface%20B%20%7B%20t%3A%20'B'%3B%20%7D%3B%0D%0Atype%20u%20%3D%20A%20%7C%20B%3B%0D%0A%0D%0Aconst%20tmp%20%3D%20%7B%20t%3A%20'B'%20%7D%20as%20u%3B%0D%0A%0D%0Aswitch(tmp.t)%20%7B%0D%0A%20%20case%20'A'%3A%20break%3B%0D%0A%20%20case%20'B'%3A%20break%3B%0D%0A%7D)\r\n* [classes](https://www.typescriptlang.org/play/#src=class%20A%20%7B%0D%0A%20%20get()%20%7B%20return%20%22A%22%3B%20%7D%0D%0A%7D%0D%0A%0D%0Aclass%20B%20%7B%0D%0A%20%20get()%20%7B%20return%20%22B%22%3B%20%7D%0D%0A%7D%0D%0A%0D%0Atype%20u%20%3D%20A%20%7C%20B%3B%0D%0A%0D%0Aconst%20tmp%20%3D%20new%20B()%20as%20u%3B%0D%0A%0D%0Aswitch(tmp.get())%20%7B%0D%0A%20%20case%20'A'%3A%20break%3B%0D%0A%20%20case%20'B'%3A%20break%3B%0D%0A%7D)\r\n* [classes with generics](https://www.typescriptlang.org/play/#src=class%20Test%3CT%3E%20%7B%0D%0A%20%20constructor(public%20v%3A%20T)%20%7B%7D%0D%0A%20%20get()%20%7B%20return%20this.v%3B%20%7D%0D%0A%7D%0D%0A%0D%0Atype%20u%20%3D%20Test%3C%22A%22%3E%20%7C%20Test%3C%22B%22%3E%3B%0D%0A%0D%0Aconst%20tmp%20%3D%20new%20Test%3C%22B%22%3E(%22B%22)%20as%20u%3B%0D%0A%0D%0Aswitch(tmp.get())%20%7B%0D%0A%20%20case%20'A'%3A%20break%3B%0D%0A%20%20case%20'B'%3A%20break%3B%0D%0A%7D)\r\n\r\n**Actual behavior:**\r\n\r\nCompilation fails with error:\r\n\r\n Type '\"B\"' is not comparable to type '\"A\"'.\r\n\r\nType of `tmp.get('t')` is only `\"A\"`.\r\n\r\nProblem is not contained to string literals, but also [other types](https://www.typescriptlang.org/play/#src=class%20Test%3CProps%20extends%20Object%3E%20%7B%0D%0A%20%20constructor(public%20o%3A%20Props)%20%7B%7D%0D%0A%20%20get%3CK%20extends%20keyof%20Props%3E(k%3A%20K)%20%3A%20Props%5BK%5D%20%7B%20return%20this.o%5Bk%5D%3B%20%7D%0D%0A%7D%0D%0A%0D%0Ainterface%20Num%20%7Ba%3A%20number%3B%7D%3B%0D%0Ainterface%20Str%20%7Ba%3A%20string%3B%7D%3B%0D%0Atype%20u%20%3D%20Test%3CNum%3E%20%7C%20Test%3CStr%3E%3B%0D%0A%0D%0Aconst%20tmp%20%3D%20new%20Test%3CStr%3E(%7Ba%3A%20'test'%7D)%20as%20u%3B%0D%0A%2F%2F%20type%20is%20'number'%2C%20not%20'number%20%7C%20string'%0D%0Aconst%20res%20%3D%20tmp.get('a')%3B). Enountered this bug using immutable-js Record.get().\r\n\r\n**Playground Link:** [playground](https://www.typescriptlang.org/play/#src=class%20Test%3CProps%20extends%20Object%3E%20%7B%0D%0A%20%20constructor(public%20o%3A%20Props)%20%7B%7D%0D%0A%20%20get%3CK%20extends%20keyof%20Props%3E(k%3A%20K)%20%3A%20Props%5BK%5D%20%7B%20return%20this.o%5Bk%5D%3B%20%7D%0D%0A%7D%0D%0A%0D%0Ainterface%20A%20%7Bt%3A%20%22A%22%3B%7D%3B%0D%0Ainterface%20B%20%7Bt%3A%20%22B%22%3B%7D%3B%0D%0Atype%20u%20%3D%20Test%3CA%3E%20%7C%20Test%3CB%3E%3B%0D%0A%0D%0Aconst%20tmp%20%3D%20new%20Test%3CB%3E(%7Bt%3A%20'B'%7D)%20as%20u%3B%0D%0A%0D%0Aswitch(tmp.get('t'))%20%7B%0D%0A%20%20case%20'A'%3A%20break%3B%0D%0A%20%20case%20'B'%3A%20break%3B%0D%0A%7D%0D%0A)\r\n\r\n**Related Issues:** \u003c!-- Did you find other bugs that looked similar? --> None found","author":{"url":"https://github.com/thammi","@type":"Person","name":"thammi"},"datePublished":"2019-04-02T19:43:27Z","url":"https://github.com/microsoft/TypeScript/issues/30717"}
| 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:3717af06-c5a0-00a6-5c2a-35dd95181e63 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | C430:2D2F2F:AF8949:E469EA:6B00E7B9 |
| html-safe-nonce | 5f64684e8a8b733856819e6cf7b16d8be2d318c2c02b289b9441e96813ea7ed1 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDNDMwOjJEMkYyRjpBRjg5NDk6RTQ2OUVBOjZCMDBFN0I5IiwidmlzaXRvcl9pZCI6IjEzMjMzNTE4NTE3MjYzMzM4ODEiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | fbc067d0d1c3104eaaff6b9aaaeb40a80c6a30c04c864b049d45ee7d3a24bf8b |
| hovercard-subject-tag | issue:428408282 |
| 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/30717/issue_layout |
| twitter:image | https://opengraph.githubassets.com/0cb0426e07ffe10c4169c0ecdc8c18720acd3898f6f6678a16d834eb8f86d256/microsoft/TypeScript/issues/30717 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/0cb0426e07ffe10c4169c0ecdc8c18720acd3898f6f6678a16d834eb8f86d256/microsoft/TypeScript/issues/30717 |
| og:image:alt | TypeScript Version: 3.4.0-dev.201xxxxx Search Terms: union, index type Code class Test |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | thammi |
| 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