Title: `extends enum` test for use in conditional types · Issue #24293 · microsoft/TypeScript · GitHub
Open Graph Title: `extends enum` test for use in conditional types · Issue #24293 · microsoft/TypeScript
X Title: `extends enum` test for use in conditional types · Issue #24293 · microsoft/TypeScript
Description: Search Terms enum isEnum mapped-types conditional-types enum-test extends-enum Suggestion Rephrased from : https://stackoverflow.com/questions/50452032/typescript-enum-type-check-for-conditional-types Here's what I kinda want, but it is ...
Open Graph Description: Search Terms enum isEnum mapped-types conditional-types enum-test extends-enum Suggestion Rephrased from : https://stackoverflow.com/questions/50452032/typescript-enum-type-check-for-conditional-ty...
X Description: Search Terms enum isEnum mapped-types conditional-types enum-test extends-enum Suggestion Rephrased from : https://stackoverflow.com/questions/50452032/typescript-enum-type-check-for-conditional-ty...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/24293
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"`extends enum` test for use in conditional types","articleBody":"## Search Terms\r\n\r\nenum isEnum mapped-types conditional-types enum-test extends-enum\r\n\r\n## Suggestion\r\n\r\nRephrased from : https://stackoverflow.com/questions/50452032/typescript-enum-type-check-for-conditional-types\r\n\r\nHere's what I kinda want, but it is not syntactically valid:\r\n\r\n```typescript\r\nenum Features {\r\n \"A\" = 1,\r\n \"B\" = 2,\r\n \"C\" = 2\r\n}\r\n\r\ntype EnumOrString\u003cT> = T extends enum\r\n ? T | keyof T\r\n : T\r\n\r\ndeclare function getData(featureFilter: EnumOrString\u003ctypeof Features>[]): Features[]\r\n```\r\n\r\n`getData` takes an array of the enum values or the enum keys but returns only the enum values.\r\n\r\nI also would want to extend this to mapped types similar to `DeepPartial` so that any nested enums all get this treatment - without having to have separate hierarchies of types partitioned by Request and Response. [Example on Playground](https://www.typescriptlang.org/play/#src=export%20type%20DeepEnumish%3CT%3E%20%3D%0D%0A%20%20%20%20T%20extends%20any%5B%5D%20%3F%20DeepEnumishArray%3CT%5Bnumber%5D%3E%20%3A%0D%0A%20%20%20%20T%20extends%20enum%20%3F%20T%20%7C%20keyof%20T%20%3A%0D%0A%20%20%20%20T%20extends%20object%20%3F%20DeepEnumishObject%3CT%3E%20%3A%0D%0A%20%20%20%20T%3B%0D%0A%0D%0Aexport%20interface%20DeepEnumishArray%3CT%3E%20extends%20Array%3CDeepEnumish%3CT%3E%3E%20%7B%7D%0D%0A%0D%0Aexport%20type%20DeepEnumishObject%3CT%3E%20%3D%20%7B%0D%0A%20%20%20%20%5BP%20in%20keyof%20T%5D%3A%20DeepEnumish%3CT%5BP%5D%3E%3B%0D%0A%7D%0D%0A%0D%0Adeclare%20function%20roundtripPayload(criteria%20%3A%20DeepEnumish%3CPayload%3E)%3A%20Payload)\r\n\r\n## Use Cases\r\n\r\nI make calls to restful services that accept enum values as either the number value OR the string key, but the services always return just the number value. I could ignore the ability to send strings, but some code would have to be rewritten because it happens to cache values by these key names.\r\n\r\n## Checklist\r\n\r\nMy suggestion meets these guidelines:\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. new expression-level syntax)","author":{"url":"https://github.com/JasonKleban","@type":"Person","name":"JasonKleban"},"datePublished":"2018-05-21T15:51:27Z","url":"https://github.com/microsoft/TypeScript/issues/24293"}
| 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:448196fb-4e30-228a-e927-598bfcf7f199 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | B880:2270F4:22D18AD:2E3467A:6B018C4E |
| html-safe-nonce | c160552cbb25a06f83d115e325773501cbbb267d858ee8f1aca59a9e3ead7617 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCODgwOjIyNzBGNDoyMkQxOEFEOjJFMzQ2N0E6NkIwMThDNEUiLCJ2aXNpdG9yX2lkIjoiMzY4NTk4NzY1NTg3NjcwOTQ1NSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | e456edccddc3d10040cda376bad94a49802d2254ad9cf67ef12b1f5e5f23bea2 |
| hovercard-subject-tag | issue:324966407 |
| 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/24293/issue_layout |
| twitter:image | https://opengraph.githubassets.com/149d5073e18e01dc4fb1a602e0f6443c99181a70dd0fd51bf8044e2d049e9ff0/microsoft/TypeScript/issues/24293 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/149d5073e18e01dc4fb1a602e0f6443c99181a70dd0fd51bf8044e2d049e9ff0/microsoft/TypeScript/issues/24293 |
| og:image:alt | Search Terms enum isEnum mapped-types conditional-types enum-test extends-enum Suggestion Rephrased from : https://stackoverflow.com/questions/50452032/typescript-enum-type-check-for-conditional-ty... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | JasonKleban |
| 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 | canary-2 |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width