Title: No type narrowing for discriminated union type guards on unique symbol properties · Issue #23135 · microsoft/TypeScript · GitHub
Open Graph Title: No type narrowing for discriminated union type guards on unique symbol properties · Issue #23135 · microsoft/TypeScript
X Title: No type narrowing for discriminated union type guards on unique symbol properties · Issue #23135 · microsoft/TypeScript
Description: Note: I know about #10530 but I explain why I think this deserves its own issue in the related issues section below TypeScript Version: 2.9.0-dev.20180403 Search Terms: unique symbol, discriminated union, type guard, narrow, narrowing Co...
Open Graph Description: Note: I know about #10530 but I explain why I think this deserves its own issue in the related issues section below TypeScript Version: 2.9.0-dev.20180403 Search Terms: unique symbol, discriminated...
X Description: Note: I know about #10530 but I explain why I think this deserves its own issue in the related issues section below TypeScript Version: 2.9.0-dev.20180403 Search Terms: unique symbol, discriminated...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/23135
X: @github
Domain: github.com
IPv4 address: 140.82.114.3
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"No type narrowing for discriminated union type guards on unique symbol properties","articleBody":"***Note: I know about #10530 but I explain why I think this deserves its own issue in the related issues section below***\r\n\r\n**TypeScript Version:** 2.9.0-dev.20180403\r\n\r\n\u003c!-- Search terms you tried before logging this (so others can find this issue more easily) -->\r\n**Search Terms:** \r\nunique symbol, discriminated union, type guard, narrow, narrowing\r\n\r\n**Code**\r\n\r\n```ts\r\ndeclare const kind: unique symbol\r\n\r\ntype Square = {\r\n readonly [kind]: \"square\"\r\n length: number\r\n width: number\r\n}\r\n\r\ntype Circle = {\r\n readonly [kind]: \"circle\"\r\n radius: number\r\n}\r\n\r\ntype Shape = Square | Circle\r\n\r\nfunction doesNotNarrow(shape: Shape) {\r\n if (shape[kind] === \"square\") {\r\n console.log(shape.length, shape.width) // error length does not exist on Shape \r\n } else {\r\n console.log(shape.radius) // error radius does not exist on Shape \r\n }\r\n}\r\n```\r\n**Expected behavior:**\r\nNo errors because the type should be narrowed inside the if statements.\r\n\r\n**Actual behavior:**\r\nCompile errors because typescript does not narrow the type despite the discriminated union.\r\n\r\n**Playground Link:** \r\n[Link](https://www.typescriptlang.org/play/#src=declare%20const%20kind%3A%20unique%20symbol%0D%0A%0D%0Atype%20Square%20%3D%20%7B%0D%0A%20%20readonly%20%5Bkind%5D%3A%20%22square%22%0D%0A%20%20length%3A%20number%0D%0A%20%20width%3A%20number%0D%0A%7D%0D%0A%0D%0Atype%20Circle%20%3D%20%7B%0D%0A%20%20readonly%20%5Bkind%5D%3A%20%22circle%22%0D%0A%20%20radius%3A%20number%0D%0A%7D%0D%0A%0D%0Atype%20Shape%20%3D%20Square%20%7C%20Circle%0D%0A%0D%0Afunction%20doesNotNarrow(shape%3A%20Shape)%20%7B%0D%0A%20%20if%20(shape%5Bkind%5D%20%3D%3D%3D%20%22square%22)%20%7B%0D%0A%20%20%20%20console.log(shape.length%2C%20shape.width)%20%2F%2F%20error%20length%20does%20not%20exist%20on%20Shape%20%0D%0A%20%20%7D%20else%20%7B%0D%0A%20%20%20%20console.log(shape.radius)%20%2F%2F%20error%20radius%20does%20not%20exist%20on%20Shape%20%0D%0A%20%20%7D%0D%0A%7D)\r\n\r\n**Related Issues:**\r\n\r\n- #10530 _Discriminant property type guard not applied with bracket notation_ \r\n\r\nI know this looks like a straight duplicate but I don't think it should be considered one because symbols aren't mentioned anywhere in that issue or in the titles of any of its duplicates, and `unique symbol` did not exist when that issue was originally considered.\r\n\r\nThe only reason that #10530 hasn't been fixed is because it caused enough of a performance slowdown that it wasn't considered a worthwhile tradeoff. I'm hoping that limiting this to only `unique symbol`s will have a small enough performance impact to make it worthwhile this time.\r\n\r\nUnlike most string keys, symbols can only be used with bracket notation so I think the value proposition here is much higher than it was for string keys. Plus it seems like using a symbol as a discriminant property that doesn't have to worry about conflicting with other property names is one of the most natural use cases for symbols.","author":{"url":"https://github.com/kpdonn","@type":"Person","name":"kpdonn"},"datePublished":"2018-04-04T08:49:29Z","url":"https://github.com/microsoft/TypeScript/issues/23135"}
| 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:e9264a41-424c-e5de-7bce-e1eb7c0781a2 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | E130:B0736:65650D:902BD6:6B046FC4 |
| html-safe-nonce | 54ed27d24949b7b54b0f78d9bcd57083a46a834cbd121b38009f7e2fbf1fd7e9 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFMTMwOkIwNzM2OjY1NjUwRDo5MDJCRDY6NkIwNDZGQzQiLCJ2aXNpdG9yX2lkIjoiMjY3MjcxNDU4MTk0ODAwMjI0NCIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 70def36f537b1611f4701f3fd79f8b2de4557e877cf688ca3070314addf08cdd |
| hovercard-subject-tag | issue:311138905 |
| 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/23135/issue_layout |
| twitter:image | https://opengraph.githubassets.com/0c8db294ea24dcbfc90bcde0acf1fc068bf9d2199ef960a116d6fe8456350b70/microsoft/TypeScript/issues/23135 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/0c8db294ea24dcbfc90bcde0acf1fc068bf9d2199ef960a116d6fe8456350b70/microsoft/TypeScript/issues/23135 |
| og:image:alt | Note: I know about #10530 but I explain why I think this deserves its own issue in the related issues section below TypeScript Version: 2.9.0-dev.20180403 Search Terms: unique symbol, discriminated... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | kpdonn |
| 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 | canary-2 |
| 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