Title: Assertions involving values from dictionary literals aren't properly checked · Issue #51270 · microsoft/TypeScript · GitHub
Open Graph Title: Assertions involving values from dictionary literals aren't properly checked · Issue #51270 · microsoft/TypeScript
X Title: Assertions involving values from dictionary literals aren't properly checked · Issue #51270 · microsoft/TypeScript
Description: Bug Report 🔎 Search Terms type assertion keyof, "type string is not assignable",'string' as keyof 🕗 Version & Regression Information This is the behavior in every version I tried, and I reviewed the FAQ for entries about type casting ⏯ P...
Open Graph Description: Bug Report 🔎 Search Terms type assertion keyof, "type string is not assignable",'string' as keyof 🕗 Version & Regression Information This is the behavior in every version I tried, and I reviewed th...
X Description: Bug Report 🔎 Search Terms type assertion keyof, "type string is not assignable",'string' as keyof 🕗 Version & Regression Information This is the behavior in every version I tr...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/51270
X: @github
Domain: github.com
IPv4 address: 140.82.112.3
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Assertions involving values from dictionary literals aren't properly checked","articleBody":"# Bug Report\r\n\r\n\u003c!--\r\n Please fill in each section completely. Thank you!\r\n-->\r\n\r\n### 🔎 Search Terms\r\n\r\n\u003c!--\r\n What search terms did you use when trying to find an existing bug report?\r\n List them here so people in the future can find this one more easily.\r\n-->\r\n`type assertion keyof`, `\"type string is not assignable\"`,`'string' as keyof`\r\n\r\n### 🕗 Version & Regression Information\r\n\r\n\u003c!-- When did you start seeing this bug occur?\r\n\r\n\"Bugs\" that have existed in TS for a long time are very likely to be FAQs; refer to\r\n https://github.com/Microsoft/TypeScript/wiki/FAQ#common-bugs-that-arent-bugs\r\n\r\nIf possible, please try testing the nightly version of TS to see if it's already been fixed.\r\nFor npm: `typescript@next`\r\nThis is also the 'Nightly' version in the playground: http://www.typescriptlang.org/play/?ts=Nightly\r\n\r\nNote: The TypeScript Playground can be used to try older versions of TypeScript.\r\n\r\nPlease keep and fill in the line that best applies:\r\n-->\r\n- This is the behavior in every version I tried, and I reviewed the FAQ for entries about __type casting__\r\n\r\n### ⏯ Playground Link\r\n\r\n\u003c!--\r\n A link to a TypeScript Playground \"Share\" link which shows this behavior\r\n\r\n The TypeScript Workbench can be used for more complex setups, try\r\n https://www.typescriptlang.org/dev/bug-workbench/\r\n\r\n As a last resort, you can link to a repo, but these will be slower for us to investigate.\r\n-->\r\n[Playground link with relevant code](https://www.typescriptlang.org/play?ts=4.8.4#code/C4TwDgpgBACglgJwIbGgXigcgDIFcBm+ImUAPlgFJIDGA1gMphIIID2A7ieZgEIA2NWgCMIzACaYA3AChp+XADtqwOKwVQ+rAObxkqABRhEKCAC5Yx1AEooAb2lRHUamoDOrPhAB0mrYcsQVjIAvrIA9GFQAMJIrtAADOagkNQAFhB0rs6sLBnAfCDSvrom+jgERJhBRdolBpgKrMBQSFBGehBVMhFh4ZExcVAAjObscGIQCllicMqqCswgUABuSHy40MmbrFCuwAhwCloANFBacMuHWlAQLDlQ+PdCTak1OgH6tmDm5YTEwV4wNVih8vj9Gs1Wu0TJgAUDuhE+tFYtAAEzmMSsCCuBSYZouAC2YAEh1OQlwzUx2NxkOUuDWBWc6ToD3uwHS2Sm+yQh2A0giUAAIkgjrcoAAvNQQACEbzqEE+3yweD+JFiFg6cOBtVBSoaTRabQCaqy8q1CN6-P6KKgAGYkuAMszaFkXLllAU5R9FT8VZVgi0smCNSZglZAdr3h19D6sBDDdDULDA3YlWbw-CkQNoAAWB0pZ2unIIPKekHR4O-Soplxc81eit6+NQ401tzAes9LM2gCsGKxOLx2SJJIUZIpUCpg9pwHpfEZaQytFZCCg7Ogtb2yF5VqFIq0YslChlDdKlb9xDbXJTZojMnLZ6bBpbHRNnL2N4C9d32agADZ8ydJci3dfJCgfAxYyrf4rw-dVg3TO9T0gytmyNV8A3VTdISDNMvwzaofxtAB2QDF0ybJQM9bD32AC8oAwaCpGkGjsIAOSaeUGLjZ90JhZCFWwi9I3lfR2M4gJCIFX8AA5RnGSYsmAHYtyuU4QDgCA+BmI4bjuVdHlXZ52V3fQACVRHcBYhE8KARFSJBLjZHYRCgVIpT2KwiggZoCQpJAbIgejGIvZjPF8-zAo44AuMYtDE06e8dWjPzgACzxhKSqNSlS9KIGi+UpOtQYAE4yMLSiSw9cCfKgXLAoAFUdMQL3MWLlQqYh71q+rPCayAxAKgI2oCbj9UhPikxkIA)\r\n\r\n### 💻 Code\r\n\r\n\u003c!-- Please post the relevant code sample here as well-->\r\n```ts\r\ntype Pirate = 'Luffy' | 'JackSparrow' | 'Blackbeard';\r\nfunction logPirate(pirate: Pirate) { console.log(pirate); }\r\n\r\n// Typechecks correctly\r\nlogPirate(({p: 'Luffy'} as {p: Pirate}).p);\r\nlogPirate(({p: 'not a pirate'} as {p: Pirate}).p);\r\n\r\n// Doesn't complain, but doesn't actually check for the constraint\r\n// Dangerous!\r\nlogPirate({p: 'Luffy' as Pirate}.p);\r\nlogPirate({p: 'not a pirate' as Pirate}.p);\r\n```\r\n\r\n### 🙁 Actual behavior\r\n\r\n\u003c!-- What happened, and why it was wrong -->\r\n\r\nCasting a dictionary field that can’t be correct raises no compiler error nor warnings.\r\n\r\n### 🙂 Expected behavior\r\n\r\n\u003c!-- What you expected to happen instead, and why -->\r\n\r\nCasting a dictionary field that can’t be correct raises a compiler error OR needlessly risky casts raise warnings.\r\n","author":{"url":"https://github.com/Rafer45","@type":"Person","name":"Rafer45"},"datePublished":"2022-10-21T23:10:49Z","url":"https://github.com/microsoft/TypeScript/issues/51270"}
| 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:54f5fc80-37dc-b158-9ec6-260e2a217cee |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | E044:3FAE88:2EA742:3FD15D:6B04BA01 |
| html-safe-nonce | 93da861e865a23cb950fd5796ed559593e585f25d155d77caec2637f2cd57be8 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFMDQ0OjNGQUU4ODoyRUE3NDI6M0ZEMTVEOjZCMDRCQTAxIiwidmlzaXRvcl9pZCI6IjU4MzU0OTk5NTQzNjk3NzIwMzMiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | fc1db6617d9a4956c75d10c7cc91d30c399249adc55ea00b55b39b009aa94bde |
| hovercard-subject-tag | issue:1418948867 |
| 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/51270/issue_layout |
| twitter:image | https://opengraph.githubassets.com/2342a30014ed784799928bdd8601a43472117f737733a6be072ba27e0d124e5b/microsoft/TypeScript/issues/51270 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/2342a30014ed784799928bdd8601a43472117f737733a6be072ba27e0d124e5b/microsoft/TypeScript/issues/51270 |
| og:image:alt | Bug Report 🔎 Search Terms type assertion keyof, "type string is not assignable",'string' as keyof 🕗 Version & Regression Information This is the behavior in every version I tried, and I reviewed th... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | Rafer45 |
| hostname | github.com |
| expected-hostname | github.com |
| None | 378bd702ac792b3550e484ad39d9a898fcd25bb52ba6962b0c9cc457c1ea71c7 |
| 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 | b032e77f1ea437ede6c115446a0a467709b468e7 |
| 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