Title: Allow use of const enums in value space when preserveConstEnums=true · Issue #51530 · microsoft/TypeScript · GitHub
Open Graph Title: Allow use of const enums in value space when preserveConstEnums=true · Issue #51530 · microsoft/TypeScript
X Title: Allow use of const enums in value space when preserveConstEnums=true · Issue #51530 · microsoft/TypeScript
Description: Background The TypeScript compiler uses const enums for the performance benefit of inlining, but then enables preserveConstEnums and modifies the output d.ts files to "lie" to API consumers and say that the enum is not const. This, and "...
Open Graph Description: Background The TypeScript compiler uses const enums for the performance benefit of inlining, but then enables preserveConstEnums and modifies the output d.ts files to "lie" to API consumers and say...
X Description: Background The TypeScript compiler uses const enums for the performance benefit of inlining, but then enables preserveConstEnums and modifies the output d.ts files to "lie" to API consume...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/51530
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Allow use of const enums in value space when preserveConstEnums=true","articleBody":"## Background\r\n\r\nThe TypeScript compiler uses const enums for the performance benefit of inlining, but then enables `preserveConstEnums` and modifies the output d.ts files to \"lie\" to API consumers and say that the enum is not const. This, and \"just don't export const enums\" are the recommended methods for library authors; the emitted code for const vs non-const enums is identical with the flag enabled.\r\n\r\nInternally, however, we use the enum values at runtime for Debug helpers. In order to make this work, we must write code like:\r\n\r\n```ts\r\nexport function formatSyntaxKind(kind: SyntaxKind | undefined): string {\r\n return formatEnum(kind, (ts as any).SyntaxKind, /*isFlags*/ false);\r\n}\r\n```\r\n\r\nBut, we cannot write:\r\n\r\n```ts\r\nexport function formatSyntaxKind(kind: SyntaxKind | undefined): string {\r\n return formatEnum(kind, SyntaxKind, /*isFlags*/ false);\r\n}\r\n// or\r\nexport function formatSyntaxKind(kind: SyntaxKind | undefined): string {\r\n return formatEnum(kind, (SyntaxKind as any), /*isFlags*/ false);\r\n}\r\n```\r\n\r\nWithout using `// @ts-ignore-error` to silence:\r\n\r\n> 'const' enums can only be used in property or index access expressions or the right hand side of an import declaration or export assignment or type query\r\n\r\n## Proposal\r\n\r\nDon't issue the above error when `preserveConstEnums=true`.\r\n\r\nI believe that this change should be safe because this flag explicitly forces tsc to emit enums as though they had been written as non-const; not issuing the error lets you use it as a value.\r\n\r\n## Motivation\r\n\r\nIn the codebase pre-modules, `ts` was the namespace and was sometimes used intentionally, but now, this `ts as any` pattern is one of the only reasons why our codebase internally needs to refer to the namespace barrels and can't direct import everything everywhere.\r\n\r\nIf this error were disabled when `preserveConstEnums=true`, however, we could just use the enums directly and not have to jump through hoops (and potentially pull enum formatting out and drop the helpers, removing a cycle between debug and types).\r\n\r\n## Alternative considered\r\n\r\nAnother alternative to this (for the TS compiler itself) is to just not use const enums. This works because our current outputs are produced by esbuild, which doesn't care at all about whether or not enums are const; it will inline them if it can, and it will produce a non-const enum object if the enum itself is needed in value space. But, if we do _any_ other kind of build that isn't \"esbuild on src\" (e.g. change bundlers, run the bundler on tsc outputs, etc), using non-const enums would cause a performance regression. I also have no idea if there are behaviors one can have in const enums and not in regular enums, though I suspect there aren't any.\r\n\r\n## Related\r\n\r\n- #31353\r\n- #37282 (in that this proposal would effectively declare we won't do it, and will stick to the current behavior)\r\n- Probably some other issue I didn't find","author":{"url":"https://github.com/jakebailey","@type":"Person","name":"jakebailey"},"datePublished":"2022-11-14T22:49:17Z","url":"https://github.com/microsoft/TypeScript/issues/51530"}
| 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:ef2bc851-b33a-6ea4-b55a-428824a7f26b |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 98C2:2A7C1C:B4A92:EED70:6B0181A5 |
| html-safe-nonce | c0536e6652e97fb3b1052add32321214de48e134c6a6854a06178e58a23210d4 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5OEMyOjJBN0MxQzpCNEE5MjpFRUQ3MDo2QjAxODFBNSIsInZpc2l0b3JfaWQiOiI0ODE4OTA2MzQ2NzM0ODQ2MzczIiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 2d27d358becf981c8415858577ca18d958720ac01342a53b6665e4fc8981d786 |
| hovercard-subject-tag | issue:1448882772 |
| 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/51530/issue_layout |
| twitter:image | https://opengraph.githubassets.com/d5d732bf7f6de2d6b6cf0d41f3333063c268d713a18468b6af55fbd86abc42a9/microsoft/TypeScript/issues/51530 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/d5d732bf7f6de2d6b6cf0d41f3333063c268d713a18468b6af55fbd86abc42a9/microsoft/TypeScript/issues/51530 |
| og:image:alt | Background The TypeScript compiler uses const enums for the performance benefit of inlining, but then enables preserveConstEnums and modifies the output d.ts files to "lie" to API consumers and say... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | jakebailey |
| 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