Title: unique symbol incorrectly widens to symbol when assigning to a const or readonly property · Issue #23263 · microsoft/TypeScript · GitHub
Open Graph Title: unique symbol incorrectly widens to symbol when assigning to a const or readonly property · Issue #23263 · microsoft/TypeScript
X Title: unique symbol incorrectly widens to symbol when assigning to a const or readonly property · Issue #23263 · microsoft/TypeScript
Description: TypeScript is a superset of JavaScript that compiles to clean JavaScript output. - unique symbol incorrectly widens to symbol when assigning to a const or readonly property · Issue #23263 · microsoft/TypeScript
Open Graph Description: TypeScript is a superset of JavaScript that compiles to clean JavaScript output. - unique symbol incorrectly widens to symbol when assigning to a const or readonly property · Issue #23263 · microso...
X Description: TypeScript is a superset of JavaScript that compiles to clean JavaScript output. - unique symbol incorrectly widens to symbol when assigning to a const or readonly property · Issue #23263 · microso...
Opengraph URL: https://github.com/microsoft/TypeScript
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"unique symbol incorrectly widens to symbol when assigning to a const or readonly property","articleBody":"**Search Terms:** \r\nunique symbol, literal, widen, reassign, const, readonly\r\n\r\n**Code**\r\n\r\n```ts\r\nconst t1 = Symbol(\"t1\")\r\nconst t1Copy = t1\r\nconst result1: typeof t1 = t1 // no error\r\nconst result2: typeof t1 = t1Copy // error: type symbol not assignable to unique symbol\r\n\r\nclass T2 {\r\n static readonly sym = Symbol(\"T2\")\r\n readonly kind = T2.sym\r\n}\r\n\r\nconst inst = new T2()\r\nconst result3: typeof T2.sym = T2.sym // no error\r\nconst result4: typeof T2.sym = inst.kind // error: type symbol not assignable to unique symbol\r\n\r\ndeclare function genericIdentity\u003cT>(arg: T): T\r\nconst unexpectedError: typeof t1 = genericIdentity(t1)\r\n\r\ndeclare function genericSymbolNested\u003cT extends symbol>(arg: T): { prop: T }\r\nconst unexpectedError2: { prop: typeof t1 } = genericSymbolNested(t1)\r\n```\r\n\r\n**Expected behavior:**\r\nNo errors. `t1Copy` and `inst.kind` should retain their respective literal `unique symbol` types the same way string literals would because they are declared with `const` and `readonly`.\r\n\r\n**Actual behavior:**\r\nErrors on `result2` and `result4` because `t1Copy` and `inst.kind` are widened to `symbol`. When assigning a `unique symbol` variable to a new variable the type is always widened to `symbol` unless an explicit type annotation is added.\r\n\r\n**Playground Link:** \r\n[Link](https://www.typescriptlang.org/play/#src=const%20t1%20%3D%20Symbol(%22t1%22)%0D%0Aconst%20t1Copy%20%3D%20t1%0D%0Aconst%20result1%3A%20typeof%20t1%20%3D%20t1%20%2F%2F%20no%20error%0D%0Aconst%20result2%3A%20typeof%20t1%20%3D%20t1Copy%20%2F%2F%20error%3A%20type%20symbol%20not%20assignable%20to%20unique%20symbol%0D%0A%0D%0Aclass%20T2%20%7B%0D%0A%20%20%20%20static%20readonly%20sym%20%3D%20Symbol(%22T2%22)%0D%0A%20%20%20%20readonly%20kind%20%3D%20T2.sym%0D%0A%7D%0D%0A%0D%0Aconst%20inst%20%3D%20new%20T2()%0D%0Aconst%20result3%3A%20typeof%20T2.sym%20%3D%20T2.sym%20%2F%2F%20no%20error%0D%0Aconst%20result4%3A%20typeof%20T2.sym%20%3D%20inst.kind%20%2F%2F%20error%3A%20type%20symbol%20not%20assignable%20to%20unique%20symbol%0D%0A%0D%0Adeclare%20function%20genericIdentity%3CT%3E(arg%3A%20T)%3A%20T%0D%0Aconst%20unexpectedError%3A%20typeof%20t1%20%3D%20genericIdentity(t1)%0D%0A%0D%0Adeclare%20function%20genericSymbolNested%3CT%20extends%20symbol%3E(arg%3A%20T)%3A%20%7B%20prop%3A%20T%20%7D%0D%0Aconst%20unexpectedError2%3A%20%7B%20prop%3A%20typeof%20t1%20%7D%20%3D%20genericSymbolNested(t1)%0D%0A)\r\n\r\n**Related Issues:**\r\nDid not notice any similar bug reports, but the design notes from #17501 seem to confirm that the current behavior is a bug:\r\n\r\n> - Like literal types, these types contain \"freshness\" which can be widened away to produce a \"regular\" type.\r\n> - When you alias a symbolic const with another const, it has the type `typeof [[that original variable]]`.\r\n\r\nWhich implies they should work like other literal types which would not be widened in the example above.","author":{"url":"https://github.com/kpdonn","@type":"Person","name":"kpdonn"},"datePublished":"2018-04-09T09:03:43Z","url":"https://github.com/microsoft/TypeScript/issues/23263"}
| 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 | /:user_id/:repository/issues/:id(.:format) |
| route-controller | issues |
| route-action | show |
| fetch-nonce | v2:0ff5c894-1e59-273a-6bda-99ea4109e3a3 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 9FB8:134361:D5690A:119E7D7:6B00F973 |
| html-safe-nonce | 2bd73bd635d6c8eda62d65fb583de603425b10dbb14ca4eec70dc405b0f55878 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5RkI4OjEzNDM2MTpENTY5MEE6MTE5RTdENzo2QjAwRjk3MyIsInZpc2l0b3JfaWQiOiIyNDU3MTg0Mzk0NjM4MTE0NDMiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | f135bf18b62872e449d1b71c6c025aadbd8e54f4f9104f93cf3601fffe4efc1c |
| hovercard-subject-tag | repository:20929025 |
| 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/microsoft/TypeScript/issues/23263 |
| twitter:image | https://opengraph.githubassets.com/7f74c95cbe889dbb3d9b1bf9b33e028a73710cb2fdd6519734f41a069c8c2bfa/microsoft/TypeScript |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/7f74c95cbe889dbb3d9b1bf9b33e028a73710cb2fdd6519734f41a069c8c2bfa/microsoft/TypeScript |
| og:image:alt | TypeScript is a superset of JavaScript that compiles to clean JavaScript output. - unique symbol incorrectly widens to symbol when assigning to a const or readonly property · Issue #23263 · microso... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| hostname | github.com |
| expected-hostname | github.com |
| None | e208d86428ec00a57ade75058166297440e8128a01e5287c31a53dc042bbf187 |
| turbo-cache-control | no-cache |
| 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 | 7dafd017fb7809a839c443dba08622af38e63c09 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width