Title: Type guard not applied based on indexed access with string literal · Issue #26232 · microsoft/TypeScript · GitHub
Open Graph Title: Type guard not applied based on indexed access with string literal · Issue #26232 · microsoft/TypeScript
X Title: Type guard not applied based on indexed access with string literal · Issue #26232 · microsoft/TypeScript
Description: TypeScript Version: 3.1.0-dev.20180804 Search Terms: dash, bracket Code Very similar to documentation example code, but with a discriminant containing a dash. Likely not because of the dash itself, but because the lookup is using s['kind...
Open Graph Description: TypeScript Version: 3.1.0-dev.20180804 Search Terms: dash, bracket Code Very similar to documentation example code, but with a discriminant containing a dash. Likely not because of the dash itself,...
X Description: TypeScript Version: 3.1.0-dev.20180804 Search Terms: dash, bracket Code Very similar to documentation example code, but with a discriminant containing a dash. Likely not because of the dash itself,...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/26232
X: @github
Domain: github.com
IPv4 address: 140.82.114.3
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Type guard not applied based on indexed access with string literal","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. Even if you think you've found a *bug*, please read the FAQ first, especially the Common \"Bugs\" That Aren't Bugs section!\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: https://github.com/Microsoft/TypeScript/wiki/FAQ\r\n\r\nPlease fill in the *entire* template below.\r\n-->\r\n\r\n\u003c!-- Please try to reproduce the issue with `typescript@next`. It may have already been fixed. -->\r\n**TypeScript Version:** 3.1.0-dev.20180804\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\ndash, bracket\r\n\r\n**Code**\r\n\r\nVery similar to documentation example code, but with a discriminant containing a dash. Likely not because of the dash itself, but because the lookup is using `s['kind-of']` instead of `s.kind`.\r\n\r\n```ts\r\ninterface Square {\r\n 'kind-of': 'square';\r\n size: number;\r\n}\r\n\r\ninterface Rectangle {\r\n 'kind-of': 'rectangle';\r\n width: number;\r\n height: number;\r\n}\r\ntype Shape = Square | Rectangle;\r\n\r\nfunction area(s: Shape): number {\r\n if (s['kind-of'] === 'square') {\r\n // Now TypeScript *knows* that `s` must be a square ;)\r\n // So you can use its members safely :)\r\n return s.size * s.size;\r\n } else {\r\n // Wasn't a square? So TypeScript will figure out that it must be a Rectangle ;)\r\n // So you can use its members safely :)\r\n return s.width * s.height;\r\n }\r\n}\r\n```\r\n\r\n**Expected behavior:**\r\n\r\nNo errors\r\n\r\n**Actual behavior:**\r\n\r\n```\r\ntest.ts:17:14 - error TS2339: Property 'size' does not exist on type 'Shape'.\r\n Property 'size' does not exist on type 'Rectangle'.\r\n\r\n17 return s.size * s.size;\r\n ~~~~\r\n\r\ntest.ts:17:23 - error TS2339: Property 'size' does not exist on type 'Shape'.\r\n Property 'size' does not exist on type 'Rectangle'.\r\n\r\n17 return s.size * s.size;\r\n ~~~~\r\n\r\ntest.ts:21:14 - error TS2339: Property 'width' does not exist on type 'Shape'.\r\n Property 'width' does not exist on type 'Square'.\r\n\r\n21 return s.width * s.height;\r\n ~~~~~\r\n\r\ntest.ts:21:24 - error TS2339: Property 'height' does not exist on type 'Shape'.\r\n Property 'height' does not exist on type 'Square'.\r\n\r\n21 return s.width * s.height;\r\n ~~~~~~\r\n```\r\n\r\nMy real world use case is to check type of AWS Cloudwatch event using the `detail-type` field on the event, so can't just change the name to get around the restriction easily.","author":{"url":"https://github.com/dalen","@type":"Person","name":"dalen"},"datePublished":"2018-08-06T15:20:55Z","url":"https://github.com/microsoft/TypeScript/issues/26232"}
| 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:aa3d54be-e19e-9d10-2dcd-8ee0958eaf91 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | C522:C29A4:2351FF:2FA427:6B0499AC |
| html-safe-nonce | fe71c3c9b5abc27270da3541a7ea2bed71b39fb9506f8471bec8d6b0b1cb318c |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDNTIyOkMyOUE0OjIzNTFGRjoyRkE0Mjc6NkIwNDk5QUMiLCJ2aXNpdG9yX2lkIjoiNDU3MjIzMjc4NTY0OTI0NDU4OCIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 385c1673782e88faf62bca4eaebdc13760b72e0e4d882c2e7953bbb42df74bf7 |
| hovercard-subject-tag | issue:347969132 |
| 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/26232/issue_layout |
| twitter:image | https://opengraph.githubassets.com/590bb1220ff83b04d435276ffdeda7a69c56089e0a0e5d84761126c1e07b7fd1/microsoft/TypeScript/issues/26232 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/590bb1220ff83b04d435276ffdeda7a69c56089e0a0e5d84761126c1e07b7fd1/microsoft/TypeScript/issues/26232 |
| og:image:alt | TypeScript Version: 3.1.0-dev.20180804 Search Terms: dash, bracket Code Very similar to documentation example code, but with a discriminant containing a dash. Likely not because of the dash itself,... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | dalen |
| hostname | github.com |
| expected-hostname | github.com |
| None | 9784b6f42e2d5eac4e394fa88cad134c0dc5b6662b4ff2787a1ed1ef5a96331d |
| 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 | 3fd14143b75b2af7b76bbd23b069867d091adfcc |
| 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