Title: In JS, type unioning of this-assignments has undesired behaviour · Issue #22792 · microsoft/TypeScript · GitHub
Open Graph Title: In JS, type unioning of this-assignments has undesired behaviour · Issue #22792 · microsoft/TypeScript
X Title: In JS, type unioning of this-assignments has undesired behaviour · Issue #22792 · microsoft/TypeScript
Description: function A() { this.p = 0 } A.prototype.m = function(x) { this.p = x } var a = new A() a.p // p: any Expected behavior: a.p: number Actual behavior: a.p: any This happens because we treat all assignments to the property as declarations a...
Open Graph Description: function A() { this.p = 0 } A.prototype.m = function(x) { this.p = x } var a = new A() a.p // p: any Expected behavior: a.p: number Actual behavior: a.p: any This happens because we treat all assig...
X Description: function A() { this.p = 0 } A.prototype.m = function(x) { this.p = x } var a = new A() a.p // p: any Expected behavior: a.p: number Actual behavior: a.p: any This happens because we treat all assig...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/22792
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"In JS, type unioning of this-assignments has undesired behaviour","articleBody":"```ts\r\nfunction A() {\r\n this.p = 0\r\n}\r\nA.prototype.m = function(x) {\r\n this.p = x\r\n}\r\nvar a = new A()\r\na.p // p: any\r\n```\r\n\r\n**Expected behavior:**\r\n`a.p: number`\r\n\r\n**Actual behavior:**\r\n`a.p: any`\r\n\r\nThis happens because we treat all assignments to the property as declarations and then union their types. And `any | T | ... === any`\r\n\r\nBut compare:\r\n\r\n```ts\r\nfunction A() {\r\n this.ps = []\r\n this.maybe = undefined\r\n}\r\nA.prototype.init = function(x) {\r\n this.ps = [1,2,3]\r\n this.maybe = 'just'\r\n}\r\nvar a = new A()\r\na.ps // ps: number[]\r\na.maybe // maybe: string | undefined\r\n```\r\n\r\nWhen the initialiser in the constructor has an uninformative type, the unioning can result in a much better type, or at least a less bad type (any). But perhaps the fix here is to handle these uninformative initialisers differently.","author":{"url":"https://github.com/sandersn","@type":"Person","name":"sandersn"},"datePublished":"2018-03-22T16:27:05Z","url":"https://github.com/microsoft/TypeScript/issues/22792"}
| 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:93edb16d-5d15-44c6-6dfa-ac32ca9fc817 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | BAE4:323204:EF991A:13CEED0:6B01D6E0 |
| html-safe-nonce | 79e5e7b6fd29c5a6c7cc65ca59ccf9a52fe28792a14d7ec4ba27ee44064a7358 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCQUU0OjMyMzIwNDpFRjk5MUE6MTNDRUVEMDo2QjAxRDZFMCIsInZpc2l0b3JfaWQiOiI1MTA0Njk0NDk3MTA3ODkxOTM3IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 8e17a70c66245488a1c3566a70bb213d0affa834148e9735f1b1d4d3022fcddc |
| hovercard-subject-tag | issue:307718646 |
| 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/22792/issue_layout |
| twitter:image | https://opengraph.githubassets.com/4ddcc3253ead373e3613911cdfebecdb2be405c56ad4b57bc02601d7b794bee0/microsoft/TypeScript/issues/22792 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/4ddcc3253ead373e3613911cdfebecdb2be405c56ad4b57bc02601d7b794bee0/microsoft/TypeScript/issues/22792 |
| og:image:alt | function A() { this.p = 0 } A.prototype.m = function(x) { this.p = x } var a = new A() a.p // p: any Expected behavior: a.p: number Actual behavior: a.p: any This happens because we treat all assig... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | sandersn |
| 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