Title: Allow const assertion to be use on enum types · Issue #30690 · microsoft/TypeScript · GitHub
Open Graph Title: Allow const assertion to be use on enum types · Issue #30690 · microsoft/TypeScript
X Title: Allow const assertion to be use on enum types · Issue #30690 · microsoft/TypeScript
Description: Search Terms A 'const' assertion can only be applied to a string, number, boolean, array, or object literal Suggestion Allow const assertion to be use on enum types. Currently the const assertion can be applied to string and number liter...
Open Graph Description: Search Terms A 'const' assertion can only be applied to a string, number, boolean, array, or object literal Suggestion Allow const assertion to be use on enum types. Currently the const assertion c...
X Description: Search Terms A 'const' assertion can only be applied to a string, number, boolean, array, or object literal Suggestion Allow const assertion to be use on enum types. Currently the const ass...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/30690
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Allow const assertion to be use on enum 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. Please read the FAQ first, especially the \"Common Feature Requests\" section.\r\n\r\n-->\r\n\r\n## Search Terms\r\n\r\nA 'const' assertion can only be applied to a string, number, boolean, array, or object literal\r\n\r\n## Suggestion\r\n\r\nAllow const assertion to be use on enum types. Currently the const assertion can be applied to `string` and `number` literals to get the compiler to keep literal types. But enums, which also can also produce literal types are not allowed in const assertions.\r\n\r\nThis seems inconsistent.\r\n\r\n## Use Cases\r\n\r\nThe use case I wanted to use it for is creating an object that could be assigned to a discriminated union (inspired by this [SO question](https://stackoverflow.com/questions/55458995/why-does-enum-value-need-to-be-coerced-before-object-type-is-recognized-properly/55459079#55459079))\r\n\r\n## Examples\r\n\r\n\r\n```ts\r\nenum AorB { A = 'a', B = 'b' };\r\nconst arg = { type: 1 as const }; // { type: 1 }\r\nconst arg2 = { type: \"1\" as const }; // { type: \"1\" }\r\nconst arg3 = { type: AorB.A as const }; // Expected: { type: AorB.A; }, Actual: A 'const' assertion can only be applied to a string, number, boolean, array, or object literal.\r\n\r\n```\r\n\r\n\r\n\r\n## 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, 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","author":{"url":"https://github.com/dragomirtitian","@type":"Person","name":"dragomirtitian"},"datePublished":"2019-04-01T16:06:00Z","url":"https://github.com/microsoft/TypeScript/issues/30690"}
| 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:0dece331-7ee2-3aa6-50e9-746a5c041ded |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | E520:17FB7:2619A4:3004AF:6B00FF1E |
| html-safe-nonce | b5df757550145a164ca2f497f983b436207687c98d307312e6139a3c9fe34561 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFNTIwOjE3RkI3OjI2MTlBNDozMDA0QUY6NkIwMEZGMUUiLCJ2aXNpdG9yX2lkIjoiNDA5ODE0ODAxNjI3NDM0MTY2MiIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 8de178c767f969d7e50268111c1b9459809a9d29a9254d70bd6041f2e2bcc3ce |
| hovercard-subject-tag | issue:427790540 |
| 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/30690/issue_layout |
| twitter:image | https://opengraph.githubassets.com/3a8a58df61fedb3b89724c1a82e9eff70054f581a647ac7b0a970119098465b5/microsoft/TypeScript/issues/30690 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/3a8a58df61fedb3b89724c1a82e9eff70054f581a647ac7b0a970119098465b5/microsoft/TypeScript/issues/30690 |
| og:image:alt | Search Terms A 'const' assertion can only be applied to a string, number, boolean, array, or object literal Suggestion Allow const assertion to be use on enum types. Currently the const assertion c... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | dragomirtitian |
| hostname | github.com |
| expected-hostname | github.com |
| None | cb7fa109eca7d4156c889f474306b0e91336055e9adf8fd4684b61e49094d124 |
| 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 | 2b59d31a4e44f08b7af67a4b67f8fd0af3befe2a |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width