Title: Symbols produced by an alias of the Symbol constructor cannot be used as object keys in types/interfaces/classes, etc. · Issue #53282 · microsoft/TypeScript · GitHub
Open Graph Title: Symbols produced by an alias of the Symbol constructor cannot be used as object keys in types/interfaces/classes, etc. · Issue #53282 · microsoft/TypeScript
X Title: Symbols produced by an alias of the Symbol constructor cannot be used as object keys in types/interfaces/classes, etc. · Issue #53282 · microsoft/TypeScript
Description: Bug Report Symbols returned by an alias of the global Symbol constructor cannot be used as objects keys. See this playground example. In and of itself this is not a bug per se, but it can result in increase bundle size when using a minif...
Open Graph Description: Bug Report Symbols returned by an alias of the global Symbol constructor cannot be used as objects keys. See this playground example. In and of itself this is not a bug per se, but it can result in...
X Description: Bug Report Symbols returned by an alias of the global Symbol constructor cannot be used as objects keys. See this playground example. In and of itself this is not a bug per se, but it can result in...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/53282
X: @github
Domain: github.com
IPv4 address: 140.82.112.3
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Symbols produced by an alias of the Symbol constructor cannot be used as object keys in types/interfaces/classes, etc.","articleBody":"# Bug Report\r\n\r\nSymbols returned by an alias of the global Symbol constructor cannot be used as objects keys. See this [playground example](https://www.typescriptlang.org/play?ts=4.9.5&ssl=14&ssc=1&pln=13&pc=20#code/MYewdgzgLgBAyjAvPAngWwEYgDYG4CwAUEaJLHOltgPoDWApigIJLwAUAlAYadKpjjqMAQqziduvcpUEMUAYTESiRKCgAO9GABUN9AOoBLKAAsKA7AGlGrAN5EYMANrmqQ5gF0AXDDABXTHoAJwdnV1kRb18AjGDQlxkaOXko-0CQwhgAX24HKRg0AENTVgBZYpNcnnA+ADNWNhQfNNigjmaY4KQAPhh7TJhselgAD1Yi0wA6QowIRq5QseQJk0m0QzA2EYAaGABaAFYFgaWCisnsEABzLY5QoOG-ILAYEe4slUJatj2ARgAGY5AA).\r\n\r\nIn and of itself this is not a bug per se, but it can result in increase bundle size when using a minifier such as terser. Terser does not mangle global names such as Math, Symbol, etc. (see: https://github.com/terser/terser/issues/1337), so a common trick to avoid them reappearing in minified code is to assign the global name to a local variable.\r\n\r\nFor instance, consider the following typescript code that uses the globals Symbol and Math: [playground link](https://www.typescriptlang.org/play?ts=4.9.5&ssl=20&ssc=1&pln=1&pc=1#code/LAKAxg9gdgzgLgAgMoE8C2AjCAbA+gawFMUBBBAXmXS2wAoBKAblElkVUxwOICEKrOdJi2jwBNbigDC-DjQbMQoOCgAOhBABU1hAOoBLOAAs5OANLF+Ab1AIEAbVN4ipALoAuBFACumQgCdbBydJHg8vXwwAoMdqLhcpcJ8-QJAEAF9FETYEADN+WhRPZKj-emLIgIoAPgQbNIRsQkQAD34AWQBDYwA6TowYQuEGtsou3rR9KFoWgBoEAFoAVmG7UYRxox7sCABzGfog-2bvfygEFsV00FBc2gWARgAGYaA)\r\n\r\n```\r\nconst Symbol_keyA = Symbol();\r\nconst Symbol_keyB = Symbol();\r\nconst Symbol_keyC = Symbol();\r\n\r\ntype TypeWithSymbolKey = {\r\n [Symbol_keyA]: number\r\n [Symbol_keyB]: number\r\n [Symbol_keyC]: number\r\n };\r\n\r\nconst f = (y: number): number => {\r\n let x = Math.abs(y);\r\n x = Math.min(x, -5);\r\n x = Math.log(x)\r\n return x;\r\n}\r\n\r\nf(-10);\r\n```\r\nwhen compiled to javascript by TS it generates the following output:\r\n\r\n```\r\n\"use strict\";\r\nconst Symbol_keyA = Symbol();\r\nconst Symbol_keyB = Symbol();\r\nconst Symbol_keyC = Symbol();\r\nconst f = (y) => {\r\n let x = Math.abs(y);\r\n x = Math.min(x, -5);\r\n x = Math.log(x);\r\n return x;\r\n};\r\nf(-10);\r\n```\r\nand when minified using terser you get the following output (you can try this using https://try.terser.org):\r\n\r\n```\r\nSymbol(),Symbol(),Symbol();(l=>{let a=Math.abs(l);a=Math.min(a,-5),a=Math.log(a)})(-10);\r\n```\r\n\r\n\r\nNow consider this minor tweak to the original code, which aliases the global Math name to a local variable: [playground link](https://www.typescriptlang.org/play?ts=4.9.5&ssl=22&ssc=1&pln=2&pc=2#code/LAKAxg9gdgzgLgAgMoE8C2AjCAbA+gawFMUBBBAXmXS2wAoBKAblElkVUxwOICEKrOdJi2jwBNbigDC-DjQbMQoOCgAOhBABU1hAOoBLOAAs5OANLF+Ab1AIEAbVN4ipALoAuBFACumQgCdbBydJHg8vXwwAoMdqLhcpcJ8-QJAEAF9FW1YxNABDY34AWQKjLPBRRAAzfloUT2So-3oGyICKAD4EGzSEbEJEAA9+fOMAOjyMGDrhXuHKUaMxtH0oWkGAGgQAWgBWWbt5hEWx7AgAc3X6IP8B738oBEHFdNBQKtptgEYABlmgA\r\n)\r\n\r\n```\r\n\r\nconst Symbol_keyA = Symbol();\r\nconst Symbol_keyB = Symbol();\r\nconst Symbol_keyC = Symbol();\r\n\r\ntype TypeWithSymbolKey = {\r\n [Symbol_keyA]: number\r\n [Symbol_keyB]: number\r\n [Symbol_keyC]: number\r\n };\r\n\r\n const math = Math;\r\n\r\nconst f = (y: number): number => {\r\n let x = math.abs(y);\r\n x = math.min(x, -5);\r\n x = math.log(x)\r\n return x;\r\n}\r\n\r\nf(-10);\r\n```\r\nwhen compiled to javascript by TS it generates the following output:\r\n\r\n```\r\n\"use strict\";\r\nconst Symbol_keyA = Symbol();\r\nconst Symbol_keyB = Symbol();\r\nconst Symbol_keyC = Symbol();\r\nconst math = Math;\r\nconst f = (y) => {\r\n let x = math.abs(y);\r\n x = math.min(x, -5);\r\n x = math.log(x);\r\n return x;\r\n};\r\nf(-10);\r\n```\r\n\r\nwhich terser can minify to:\r\n\r\n```\r\nSymbol(),Symbol(),Symbol();const l=Math;(o=>{let b=l.abs(o);b=l.min(b,-5),b=l.log(b)})(-10);\r\n```\r\n\r\nObviously in this small example there isn't a significant difference in minified size, but in a large library that includes hundreds of calls to a top level name, this reduction can be significant. Its not uncommon to re-export math functions from an internal module to enable this type of minified output when one is focused on reducing output size.\r\n\r\nUnfortunately, for libraries written in TS that choose to use symbols for object keys, class methods, etc. the resulting output can become littered with invocations of the Symbol constructor and there is no typesafe way in typescript to work around this.\r\n\r\n### 🔎 Search Terms\r\n\r\nSymbol\r\n\r\n### 🕗 Version & Regression Information\r\n\r\n4.95\r\n\r\n- This is the behavior in every version I tried, and I reviewed the FAQ for entries about Symbol\r\n\r\n### ⏯ Playground Link\r\n\r\nSymbols returned from alias to Symbol not working as keys: [playground example](https://www.typescriptlang.org/play?ts=4.9.5&ssl=14&ssc=1&pln=13&pc=20#code/MYewdgzgLgBAyjAvPAngWwEYgDYG4CwAUEaJLHOltgPoDWApigIJLwAUAlAYadKpjjqMAQqziduvcpUEMUAYTESiRKCgAO9GABUN9AOoBLKAAsKA7AGlGrAN5EYMANrmqQ5gF0AXDDABXTHoAJwdnV1kRb18AjGDQlxkaOXko-0CQwhgAX24HKRg0AENTVgBZYpNcnnA+ADNWNhQfNNigjmaY4KQAPhh7TJhselgAD1Yi0wA6QowIRq5QseQJk0m0QzA2EYAaGABaAFYFgaWCisnsEABzLY5QoOG-ILAYEe4slUJatj2ARgAGY5AA)\r\n\r\n### 💻 Code\r\n\r\n\r\n### 🙁 Actual behavior\r\n\r\nSymbols returned by aliases of the global Symbol constructor are not unique and cannot be used as keys in interfaces and types.\r\n\r\n### 🙂 Expected behavior\r\n\r\nSymbols returned by aliases of the global Symbol constructor should be unique symbols usable as keys in interfaces and types.","author":{"url":"https://github.com/bordoley","@type":"Person","name":"bordoley"},"datePublished":"2023-03-16T01:59:51Z","url":"https://github.com/microsoft/TypeScript/issues/53282"}
| 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:b091e258-ee13-ed79-e587-89a24df3b19b |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 9F98:34B9D4:6CE377:9A7C6B:6B04708A |
| html-safe-nonce | 26d3587ea287fa6f154e05e0c2ffaaa68286be0e2ed6564680aae2699283bf05 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5Rjk4OjM0QjlENDo2Q0UzNzc6OUE3QzZCOjZCMDQ3MDhBIiwidmlzaXRvcl9pZCI6IjcyNjEyMDYzMTY2ODgxMDk3MDYiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 3d155ef367c2b61f59667fd42546a48dd4474e84cc847f0445cdac5c7c7c128d |
| hovercard-subject-tag | issue:1626553776 |
| 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/53282/issue_layout |
| twitter:image | https://opengraph.githubassets.com/9728b67200f9138be7d37c521ae1f3301ba63eeb8766d4af34dceaebf08dd817/microsoft/TypeScript/issues/53282 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/9728b67200f9138be7d37c521ae1f3301ba63eeb8766d4af34dceaebf08dd817/microsoft/TypeScript/issues/53282 |
| og:image:alt | Bug Report Symbols returned by an alias of the global Symbol constructor cannot be used as objects keys. See this playground example. In and of itself this is not a bug per se, but it can result in... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | bordoley |
| hostname | github.com |
| expected-hostname | github.com |
| None | ae8802492f12d3ba4f6b0d814054e617a2a5616b6327bf8555c10cd5b0dc2a6f |
| 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 | cfb5f24c6e01d85f70804652a5107e5cac03a161 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
| Server | github.com |
| Content-Type | text/html; charset=utf-8 |
| Cache-Control | max-age=0, private, must-revalidate |
Viewport: width=device-width