Title: Mapped types enumerating keys in `string` behave poorly · Issue #22509 · microsoft/TypeScript · GitHub
Open Graph Title: Mapped types enumerating keys in `string` behave poorly · Issue #22509 · microsoft/TypeScript
X Title: Mapped types enumerating keys in `string` behave poorly · Issue #22509 · microsoft/TypeScript
Description: TypeScript Version: 2.8.0-dev20180308 Search Terms: mapped types Code type Thing = { [K in string]: K }; var thing: Thing = new Proxy({}, { get(_, k) { return k; } }); let res = thing.x; // res should be of type 'x', but is string Playgr...
Open Graph Description: TypeScript Version: 2.8.0-dev20180308 Search Terms: mapped types Code type Thing = { [K in string]: K }; var thing: Thing = new Proxy({}, { get(_, k) { return k; } }); let res = thing.x; // res sho...
X Description: TypeScript Version: 2.8.0-dev20180308 Search Terms: mapped types Code type Thing = { [K in string]: K }; var thing: Thing = new Proxy({}, { get(_, k) { return k; } }); let res = thing.x; // res sho...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/22509
X: @github
Domain: github.com
IPv4 address: 140.82.114.4
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Mapped types enumerating keys in `string` behave poorly","articleBody":"**TypeScript Version:** 2.8.0-dev20180308\r\n**Search Terms:** mapped types\r\n**Code**\r\n\r\n```ts\r\ntype Thing = {\r\n [K in string]: K\r\n};\r\n\r\nvar thing: Thing = new Proxy({}, {\r\n get(_, k) {\r\n return k;\r\n }\r\n});\r\n\r\nlet res = thing.x; // res should be of type 'x', but is string\r\n```\r\n[Playground Link](http://www.typescriptlang.org/play/index.html#src=type%20Thing%20%3D%20%7B%0D%0A%20%20%20%20%5BK%20in%20string%5D%3A%20K%0D%0A%7D%3B%0D%0A%0D%0Avar%20thing%3A%20Thing%20%3D%20new%20Proxy(%7B%7D%2C%20%7B%0D%0A%20%20%20%20get(_%2C%20k)%20%7B%0D%0A%20%20%20%20%20%20%20%20return%20k%3B%0D%0A%20%20%20%20%7D%0D%0A%7D)%3B%0D%0A%0D%0Alet%20res%20%3D%20thing.x%3B%20%2F%2F%20res%20should%20be%20of%20type%20'x'%2C%20but%20is%20string)\r\n\r\nHere I expect `string` to behave as if it were the union of all possible string literal types. The present semantics are identical to a string indexer. IMO if we don't want map types `K in string` to do anything interesting, then the syntax should be an error and we should require people to write the indexer. \r\n\r\nFor what it's worth, ultimately I would love to type Allen's Apparatus for Method Extraction, roughly typed below:\r\n\r\n```ts\r\ntype ExtractorThing = {\r\n [K in string]: \u003cT>(base: T) => K extends keyof T ? T[K] : never;\r\n}\r\n\r\nvar extract: ExtractorThing = new Proxy({}, {\r\n get(target, prop) {\r\n \"use strict\";\r\n return (base: any) => function(this: any, ...args: any[]) {\r\n return base[prop].apply(this || base, args); \r\n }\r\n }\r\n});\r\n\r\nlet obj = {\r\n x: 0,\r\n succ() {\r\n return ++this.x;\r\n },\r\n dec() {\r\n return --this.x;\r\n }\r\n}\r\n\r\nlet succ = extract.succ(obj);\r\nlet dec = extract.dec(obj);\r\n\r\nsucc(); // 1\r\nsucc(); // 2\r\ndec(); // 1\r\ndec(); // 0\r\n```\r\n\r\nI would probably work towards putting a constraint on T such that you get a red squiggle if you pass an object without the proper method to extract, but hopefully this serves as a useful example.","author":{"url":"https://github.com/bterlson","@type":"Person","name":"bterlson"},"datePublished":"2018-03-13T16:43:20Z","url":"https://github.com/microsoft/TypeScript/issues/22509"}
| 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:168ca853-0264-10f7-e59c-3547137b3d82 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 95DA:2E648F:ACE66C:ECB47F:6B0451D1 |
| html-safe-nonce | 30460c5dc23e82b9a94b87d540123ad820619448ffc3e85198a99ac1dbac2718 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5NURBOjJFNjQ4RjpBQ0U2NkM6RUNCNDdGOjZCMDQ1MUQxIiwidmlzaXRvcl9pZCI6IjI4ODU4NDQ5ODUzMjI5NTExMjEiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 3e3063b3b395088c04c6506c635167f46f315e9dc48a0ec1ca54a7abc608a740 |
| hovercard-subject-tag | issue:304845159 |
| 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/22509/issue_layout |
| twitter:image | https://opengraph.githubassets.com/8cbc2998697bb03e16017d9a591ea84d059b0a51a897ba5bc2adc0649d67dfd8/microsoft/TypeScript/issues/22509 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/8cbc2998697bb03e16017d9a591ea84d059b0a51a897ba5bc2adc0649d67dfd8/microsoft/TypeScript/issues/22509 |
| og:image:alt | TypeScript Version: 2.8.0-dev20180308 Search Terms: mapped types Code type Thing = { [K in string]: K }; var thing: Thing = new Proxy({}, { get(_, k) { return k; } }); let res = thing.x; // res sho... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | bterlson |
| hostname | github.com |
| expected-hostname | github.com |
| None | 910ca0e260cfec6aff11407d8dc6a0d3a4cd381a2ec9ada56627b063426892a0 |
| 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 | e086f9b7e88322eb2b41885a6ad425b215737fa5 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
| Server | github.com |
| Content-Type | text/html; charset=utf-8 |
| Cache-Control | max-age=0, private, must-revalidate |
Viewport: width=device-width