Title: Support opting-out of type narrowing on assignment · Issue #49669 · microsoft/TypeScript · GitHub
Open Graph Title: Support opting-out of type narrowing on assignment · Issue #49669 · microsoft/TypeScript
X Title: Support opting-out of type narrowing on assignment · Issue #49669 · microsoft/TypeScript
Description: Suggestion 🔍 Search Terms assignment proxy assignment narrow narrow proxy ✅ Viability Checklist This wouldn't be a breaking change in existing TypeScript/JavaScript code This wouldn't change the runtime behavior of existing JavaScript co...
Open Graph Description: Suggestion 🔍 Search Terms assignment proxy assignment narrow narrow proxy ✅ Viability Checklist This wouldn't be a breaking change in existing TypeScript/JavaScript code This wouldn't change the ru...
X Description: Suggestion 🔍 Search Terms assignment proxy assignment narrow narrow proxy ✅ Viability Checklist This wouldn't be a breaking change in existing TypeScript/JavaScript code This wouldn't chang...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/49669
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Support opting-out of type narrowing on assignment","articleBody":"# Suggestion\r\n\r\n## 🔍 Search Terms\r\n\r\n- assignment proxy\r\n- assignment narrow\r\n- narrow proxy\r\n\r\n## ✅ Viability Checklist\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, new syntax sugar for JS, 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\r\n## ⭐ Suggestion\r\n\r\nI think there should be some way to tell TS to not narrow the type of a property when a new value is assigned to that property, to better work with proxies.\r\n\r\n## 📃 Motivating Example\r\n\r\nThere's currently an issue in how TS works with proxies, in that it's not able to recognize if something is a proxy or not (not even JS at runtime could do that really), so it treats property assignments always the same way.\r\n\r\nThe problem is that if I assign the value `true` to a property of a non-proxy object then it's reasonable to make the assumption that that's immediately the new value of that property, that's just how the language works (assuming no property descriptor property messing with that), but if that object is a Proxy anything at all could happen, and TS doesn't understand that.\r\n\r\n## 💻 Use Cases\r\n\r\nIn [Solid](https://www.solidjs.com) there's a type of proxied object called a \"mutable store\", and one detail about this object is that it will defer assignments to a later point in time if they happen inside a \"batch\" call, as not doing this would make the system \"glitchy\".\r\n\r\nBut TS just can't work with that, so mutable stores become unusable with TS, in the following example TS is certain that the `if` will never be entered, but as one can see from the console output it is in fact entered at runtime:\r\n\r\nhttps://playground.solidjs.com/?hash=-1767694278&version=1.4.1","author":{"url":"https://github.com/fabiospampinato","@type":"Person","name":"fabiospampinato"},"datePublished":"2022-06-24T14:45:45Z","url":"https://github.com/microsoft/TypeScript/issues/49669"}
| 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:59de1be7-39b1-0e83-2329-2d71c1836e45 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 94CC:19F84C:31CD7B9:3F4EB2F:6B01BBA3 |
| html-safe-nonce | 353b0240a248b8512cb766ceed7ebb663b6f60268779eaea5f8ab6749083d0cc |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5NENDOjE5Rjg0QzozMUNEN0I5OjNGNEVCMkY6NkIwMUJCQTMiLCJ2aXNpdG9yX2lkIjoiNTYzOTg2NjU1MTk3MzgyMTM0NyIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | dc8dd6725f169d78198af7e8239680ccb6796125db7375c895470264a8d9d321 |
| hovercard-subject-tag | issue:1283839808 |
| 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/49669/issue_layout |
| twitter:image | https://opengraph.githubassets.com/a8a68fabd7b2eb4532d11576781049fb814467653c6c34a31f2c055508563684/microsoft/TypeScript/issues/49669 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/a8a68fabd7b2eb4532d11576781049fb814467653c6c34a31f2c055508563684/microsoft/TypeScript/issues/49669 |
| og:image:alt | Suggestion 🔍 Search Terms assignment proxy assignment narrow narrow proxy ✅ Viability Checklist This wouldn't be a breaking change in existing TypeScript/JavaScript code This wouldn't change the ru... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | fabiospampinato |
| hostname | github.com |
| expected-hostname | github.com |
| None | cc5dfdd32f62ebd3223cab3af3aa2b268d3605218237326a48a99d1efd89495c |
| 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 | 8c77c9e8439031ad54c75a250694842892f693e1 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width