Title: Support inferred string-modified union types · Issue #17512 · microsoft/TypeScript · GitHub
Open Graph Title: Support inferred string-modified union types · Issue #17512 · microsoft/TypeScript
X Title: Support inferred string-modified union types · Issue #17512 · microsoft/TypeScript
Description: I ran into an interesting situation that I wanted to run by the community to see if there's a way to add a new feature into TypeScript to support. I'll grant that it's an edge-case, but it seems like something that could be kind of neat....
Open Graph Description: I ran into an interesting situation that I wanted to run by the community to see if there's a way to add a new feature into TypeScript to support. I'll grant that it's an edge-case, but it seems li...
X Description: I ran into an interesting situation that I wanted to run by the community to see if there's a way to add a new feature into TypeScript to support. I'll grant that it's an edge-case, but...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/17512
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Support inferred string-modified union types","articleBody":"I ran into an interesting situation that I wanted to run by the community to see if there's a way to add a new feature into TypeScript to support. I'll grant that it's an edge-case, but it seems like something that could be kind of neat. \r\n\r\nHere's the code:\r\n\r\n**Code**\r\n\r\n```ts\r\n\r\n// From Library 1\r\ntype status = \"success\" | \"error\" | \"warning\" | \"info\";\r\n\r\n// From Library 2\r\ntype statusColor = \"success\" | \"success-dark\" | \"error\" | \"error-dark\" | \"warning\" | \"warning-dark\" | \"info\" | \"info-dark\";\r\n\r\n// My library\r\nfunction getColorFromStatus(status: status): statusColor {\r\n\r\n // ideal case: doesn't work:\r\n return status + \"-dark\"; // Error TS2322\tType 'string' is not assignable to type 'statusColor'\r\n\r\n // compromise: compiles but not type-safe. Changes to \"status\" type won't be caught.\r\n return status + \"-dark\" as statusColor;\r\n\r\n // safe case: works but extra code + runtime penalty:\r\n switch (status) {\r\n case \"success\": return \"success-dark\";\r\n case \"error\": return \"error-dark\";\r\n case \"warning\": return \"warning-dark\";\r\n case \"info\": return \"info-dark\";\r\n }\r\n}\r\n\r\n```\r\n\r\nI think it would be neat if the expression:\r\n\r\n```ts\r\nstatus + \"-dark\"\r\n```\r\n\r\ncould be automatically inferred to be of type:\r\n\r\n```ts\r\n\"success-dark\" | \"error-dark\" | \"warning-dark\" | \"info-dark\";\r\n```\r\n\r\nIt's probably a lot more complex than it seems. And there's probably a heck of a lot of issues dealing with what happens with other string operations or if a runtime function like .toUpperCase() is called on the string, and what happens if any item in the union isn't a string, etc, etc. But still.\r\n\r\nAny thoughts?\r\n\r\n","author":{"url":"https://github.com/RonPenton","@type":"Person","name":"RonPenton"},"datePublished":"2017-07-29T22:52:54Z","url":"https://github.com/microsoft/TypeScript/issues/17512"}
| 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:d7d0c148-5cd6-e66d-5f05-00abed09a1b9 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | BE74:E75B8:5DC74:74BD0:6B00F218 |
| html-safe-nonce | 6b8d48c5498345185c39cfc884a6e8f9dd2ac822724e516acdf0cd00d914cd34 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCRTc0OkU3NUI4OjVEQzc0Ojc0QkQwOjZCMDBGMjE4IiwidmlzaXRvcl9pZCI6IjYwMjMxNjAxOTY2NTY4NTM1MjgiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | a12909f9eb3fd1b18b42816ef1ff128682307cf80aeefdfca1a4a3f5cf55cf42 |
| hovercard-subject-tag | issue:246550230 |
| 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/17512/issue_layout |
| twitter:image | https://opengraph.githubassets.com/17fc9450ccbe194a85e43f32057433c8b3209c07058b84086df51ae9b85bc7bc/microsoft/TypeScript/issues/17512 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/17fc9450ccbe194a85e43f32057433c8b3209c07058b84086df51ae9b85bc7bc/microsoft/TypeScript/issues/17512 |
| og:image:alt | I ran into an interesting situation that I wanted to run by the community to see if there's a way to add a new feature into TypeScript to support. I'll grant that it's an edge-case, but it seems li... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | RonPenton |
| hostname | github.com |
| expected-hostname | github.com |
| None | 4ef498cbabcbe3c50befc7a5b743e7072ee770b4daf0e15e54801180d1fa91fc |
| 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 | 9983dd5da06943f833c971dd5b6bc988e2858be5 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width