Title: Cannot create a unique symbol at each function call · Issue #40106 · microsoft/TypeScript · GitHub
Open Graph Title: Cannot create a unique symbol at each function call · Issue #40106 · microsoft/TypeScript
X Title: Cannot create a unique symbol at each function call · Issue #40106 · microsoft/TypeScript
Description: Search Terms Symbol, unique, generated, function call Suggestion With two functions returning unique symbol. declare function randomSymbol(): { readonly id: unique symbol }; declare function randomSymbol2(): { readonly id: unique symbol ...
Open Graph Description: Search Terms Symbol, unique, generated, function call Suggestion With two functions returning unique symbol. declare function randomSymbol(): { readonly id: unique symbol }; declare function random...
X Description: Search Terms Symbol, unique, generated, function call Suggestion With two functions returning unique symbol. declare function randomSymbol(): { readonly id: unique symbol }; declare function random...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/40106
X: @github
Domain: github.com
IPv4 address: 140.82.114.3
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Cannot create a unique symbol at each function call","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.\r\n\r\nPlease help us by doing the following steps before logging an issue:\r\n * Search: https://github.com/Microsoft/TypeScript/search?type=Issues\r\n * Read the FAQ, especially the \"Common Feature Requests\" section: https://github.com/Microsoft/TypeScript/wiki/FAQ\r\n\r\n-->\r\n\r\n## Search Terms\r\n\r\nSymbol, unique, generated, function call\r\n\r\n## Suggestion\r\n\r\nWith two functions returning `unique symbol`.\r\n\r\n```ts\r\ndeclare function randomSymbol(): { readonly id: unique symbol };\r\ndeclare function randomSymbol2(): { readonly id: unique symbol };\r\n```\r\n\r\nWhen comparing the symbol types when calling these two functions, we get a (correct) error from the compiler telling us that these types are incompatible.\r\n\r\n```ts\r\nconst a = randomSymbol();\r\nconst b = randomSymbol2();\r\n\r\na.id === a.id;\r\na.id === b.id; // TS ERROR: Expected behaviour. 👍 \r\n```\r\n\r\nIf we call the same function two times though, the types are the same.\r\n\r\n```ts\r\nconst a = randomSymbol();\r\nconst b = randomSymbol();\r\n\r\na.id === a.id;\r\na.id === b.id; // NO ERROR: But I would expect these two types to be different too 👎 \r\n```\r\n\r\nI understand that this can be expected in some cases, though there is currently no way to tell the compiler to generate a different unique symbol for each function call.\r\n\r\n## Use Cases\r\n\r\nMy current use-case is for creating two objects using the same constructor, that are structurally identical, but that would not be compatible between them (kind of opaque types).\r\n\r\nMaybe this should be through the usage of a new keyword to prevent compatibility issues with existing codebases.\r\n\r\n## Examples\r\n\r\nhttps://www.typescriptlang.org/play?#code/CYUwxgNghgTiAEAzArgOzAFwJYHtXxilWBwFsBlAT1ICMcIAKASgC54BvAkKE1CS+FmBs0WAI7IEAZ2p0I8AL4BuAFChIsBCnTY8BIiQqz6AJmZtOcHnn6Dh8UROnH5ylSvYr48MHikZ4KHgAXn1iMipaemZVb19Uf3gaELDDSLkzJlUvQIA6IRDg0Kh84Fi8gqLQmlKleAB6evgAeQBpFQV3Tzi-AKDQwnCjKMYsnPjE5IGDCJcY928SyqKKspyl4ELq2oamgEl4AHccZAhNkAAPAAdwAIwACxApBAxj+AxKG6l3nCSEYCwiEQIDgqDuOBwHRUQA\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","author":{"url":"https://github.com/kube","@type":"Person","name":"kube"},"datePublished":"2020-08-18T03:07:54Z","url":"https://github.com/microsoft/TypeScript/issues/40106"}
| 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:bd95422d-f16f-b374-70d0-5dc1151b80ff |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | B416:20D380:3FB573:5798D0:6B046D7B |
| html-safe-nonce | b6d5096136207763966ad432889fe194b9bb64ea9620e13e536b533f78201ab3 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCNDE2OjIwRDM4MDozRkI1NzM6NTc5OEQwOjZCMDQ2RDdCIiwidmlzaXRvcl9pZCI6IjEwNDQxOTQ4OTAzNDAzOTY0MTEiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 81bc892a152fba11e39b84c12bde51884fe619cf4442840bb858c7b5ffb17142 |
| hovercard-subject-tag | issue:680652996 |
| 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/40106/issue_layout |
| twitter:image | https://opengraph.githubassets.com/73263000edd90a157a77f81fae8b0eec72e05acd7cedf02116fdc0e6daf59a4c/microsoft/TypeScript/issues/40106 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/73263000edd90a157a77f81fae8b0eec72e05acd7cedf02116fdc0e6daf59a4c/microsoft/TypeScript/issues/40106 |
| og:image:alt | Search Terms Symbol, unique, generated, function call Suggestion With two functions returning unique symbol. declare function randomSymbol(): { readonly id: unique symbol }; declare function random... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | kube |
| 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