Title: Operator to produce keys of a type where the properties of that type in those keys match some type · Issue #48992 · microsoft/TypeScript · GitHub
Open Graph Title: Operator to produce keys of a type where the properties of that type in those keys match some type · Issue #48992 · microsoft/TypeScript
X Title: Operator to produce keys of a type where the properties of that type in those keys match some type · Issue #48992 · microsoft/TypeScript
Description: Suggestion 🔍 Search Terms keyof property keysOfType ✅ Viability Checklist My suggestion meets these guidelines: This wouldn't be a breaking change in existing TypeScript/JavaScript code This wouldn't change the runtime behavior of existi...
Open Graph Description: Suggestion 🔍 Search Terms keyof property keysOfType ✅ Viability Checklist My suggestion meets these guidelines: This wouldn't be a breaking change in existing TypeScript/JavaScript code This wouldn...
X Description: Suggestion 🔍 Search Terms keyof property keysOfType ✅ Viability Checklist My suggestion meets these guidelines: This wouldn't be a breaking change in existing TypeScript/JavaScript code This wo...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/48992
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Operator to produce keys of a type where the properties of that type in those keys match some type","articleBody":"# Suggestion\r\n\r\n\u003c!--\r\n Please fill in each section completely. Thank you!\r\n-->\r\n\r\n## 🔍 Search Terms\r\n\r\nkeyof property keysOfType\r\n\r\n## ✅ Viability Checklist\r\n\r\nMy suggestion meets these guidelines:\r\n\r\n* [x] This wouldn't be a breaking change in existing TypeScript/JavaScript code\r\n* [x] This wouldn't change the runtime behavior of existing JavaScript code\r\n* [x] This could be implemented without emitting different JS based on the types of the expressions\r\n* [x] This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)\r\n* [x] This feature would agree with the rest of [TypeScript's Design Goals](https://github.com/Microsoft/TypeScript/wiki/TypeScript-Design-Goals).\r\n\r\n\r\n## ⭐ Suggestion\r\n\r\nWe frequently get people writing definitions to produce the keys `K` of an object type `O` where `O[K] extends T`. Let's call this operation `KeysOfType\u003cO, T>`. Varying userland definitions include\r\n\r\n```ts\r\ntype ObjectKey\u003cO, T> = {[K in keyof O]: O[K] extends T ? K : never}[keyof O & string]\r\n// TODO: Link more definitions\r\n```\r\n\r\nThese userland definitions work in concrete cases, but can't be used to write generic functions:\r\n```ts\r\ntype ObjectKey\u003cO, T> = {[K in keyof O]: O[K] extends T ? K : never}[keyof O & string];\r\nfunction getString\u003cO extends object, K extends ObjectKey\u003cO, string>>(obj: O, key: K): string {\r\n // Can't assign ObjectKey\u003cO, K> to 'string', but this is sound by construction\r\n return obj[key];\r\n}\r\n\r\n// Demo\r\ninterface MyInterface {\r\n someString: string;\r\n someNumber: number;\r\n}\r\ndeclare const mi: MyInterface;\r\ngetString(mi, \"someString\"); // OK\r\ngetString(mi, \"someNumber\"); // Error\r\n```\r\n\r\nWe could add a new type operator or intrinsic alias `KeysOfType\u003cT, P>` that returns all `keyof T` `K` such that `T[K] extends P`. When this type indexes a generic object of type `T`, we can then know that `T[KeysOfType\u003cT, P>]` is assignable to `P`. In non-generic positions, this can be immediately resolved the same way as the userland definition.\r\n\r\n```ts\r\ntype KeysOfType\u003cO, P> = intrinsic;\r\n\r\n// Hypothetical\r\nfunction getString\u003cO extends object>(obj: O, key: KeysOfType\u003cO, string>): string {\r\n // OK, O[KeysOfType\u003cO, string>] is known to be assignable to\r\n // string because O is a subtype of O and string is assignable to string\r\n return obj[key];\r\n}\r\n\r\n// getString works the same from the outside as the userland version\r\n```\r\n\r\n## 📃 Motivating Example\r\n\r\nSee `getString` above\r\n\r\n## 💻 Use Cases\r\n\r\nThis is a frequent complaint; need link more inbound issues here\r\n\r\n#48989 ","author":{"url":"https://github.com/RyanCavanaugh","@type":"Person","name":"RyanCavanaugh"},"datePublished":"2022-05-06T16:20:40Z","url":"https://github.com/microsoft/TypeScript/issues/48992"}
| 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:4e94afef-c61d-b043-24d1-03ece14fc851 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | CC20:27D3AD:2D0E21B:3933701:6B01B4BA |
| html-safe-nonce | 34266e2b25a1bb1ded834152ab7e59a5794c5876532c46d0f071e9f9b8163867 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDQzIwOjI3RDNBRDoyRDBFMjFCOjM5MzM3MDE6NkIwMUI0QkEiLCJ2aXNpdG9yX2lkIjoiNDAzNDQzNzY2MTExMTQ2NTE0NyIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | eeeb93321752158701c59ff949b0023434e75b5b350341354d00b73e8f8054fa |
| hovercard-subject-tag | issue:1228060471 |
| 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/48992/issue_layout |
| twitter:image | https://opengraph.githubassets.com/c07a7e6a2175932f07d63b337d05f62384f584c03b34e3e03baad057b3bdc907/microsoft/TypeScript/issues/48992 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/c07a7e6a2175932f07d63b337d05f62384f584c03b34e3e03baad057b3bdc907/microsoft/TypeScript/issues/48992 |
| og:image:alt | Suggestion 🔍 Search Terms keyof property keysOfType ✅ Viability Checklist My suggestion meets these guidelines: This wouldn't be a breaking change in existing TypeScript/JavaScript code This wouldn... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | RyanCavanaugh |
| hostname | github.com |
| expected-hostname | github.com |
| None | cc5dfdd32f62ebd3223cab3af3aa2b268d3605218237326a48a99d1efd89495c |
| 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 | 8c77c9e8439031ad54c75a250694842892f693e1 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width