Title: Getter on class is present in inferred type from object-spreading instance · Issue #26279 · microsoft/TypeScript · GitHub
Open Graph Title: Getter on class is present in inferred type from object-spreading instance · Issue #26279 · microsoft/TypeScript
X Title: Getter on class is present in inferred type from object-spreading instance · Issue #26279 · microsoft/TypeScript
Description: TypeScript Version: 3.1.0-dev.20180807 Search Terms: object spread own enumerable object spread accessor getter object spread Code (playground) class Phone { get number() { return 1234; } getNumber() { return 1234; } } const phoneInstanc...
Open Graph Description: TypeScript Version: 3.1.0-dev.20180807 Search Terms: object spread own enumerable object spread accessor getter object spread Code (playground) class Phone { get number() { return 1234; } getNumber...
X Description: TypeScript Version: 3.1.0-dev.20180807 Search Terms: object spread own enumerable object spread accessor getter object spread Code (playground) class Phone { get number() { return 1234; } getNumber...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/26279
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Getter on class is present in inferred type from object-spreading instance","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.20180807\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\n- object spread own enumerable\r\n- object spread accessor\r\n- getter object spread\r\n\r\n**Code** ([playground](https://www.typescriptlang.org/play/index.html#src=class%20Phone%20%7B%0D%0A%20%20get%20number()%20%7B%0D%0A%20%20%20%20return%201234%3B%0D%0A%20%20%7D%0D%0A%0D%0A%20%20getNumber()%20%7B%0D%0A%20%20%20%20return%3B%0D%0A%20%20%7D%0D%0A%7D%0D%0A%0D%0Aconst%20phoneInstance%20%3D%20new%20Phone()%3B%0D%0Aconst%20phoneClone%20%3D%20%7B%20...phoneInstance%20%7D%3B))\r\n\r\n```ts\r\nclass Phone {\r\n get number() {\r\n return 1234;\r\n }\r\n\r\n getNumber() {\r\n return 1234;\r\n }\r\n}\r\n\r\nconst phoneInstance = new Phone();\r\nconst phoneClone = { ...phoneInstance };\r\n```\r\n\r\n**Expected behavior:**\r\nThe inferred type for `phoneClone` should be `{}`\r\n\r\n**Actual behavior:**\r\nThe inferred type for `phoneClone` is `{ number: number }`\r\n\r\n**Playground Link:** [playground](https://www.typescriptlang.org/play/index.html#src=class%20Phone%20%7B%0D%0A%20%20get%20number()%20%7B%0D%0A%20%20%20%20return%201234%3B%0D%0A%20%20%7D%0D%0A%0D%0A%20%20getNumber()%20%7B%0D%0A%20%20%20%20return%3B%0D%0A%20%20%7D%0D%0A%7D%0D%0A%0D%0Aconst%20phoneInstance%20%3D%20new%20Phone()%3B%0D%0Aconst%20phoneClone%20%3D%20%7B%20...phoneInstance%20%7D%3B)\r\n\r\n**Related Issues:** \u003c!-- Did you find other bugs that looked similar? -->\r\n- https://github.com/Microsoft/TypeScript/issues/13148\r\n- https://github.com/Microsoft/TypeScript/issues/13409\r\n- https://github.com/Microsoft/TypeScript/issues/24799\r\n\r\n---\r\n\r\n## Discussion\r\n\r\n1. I realize that spreading a class instance is not desirable.\r\n2. I also realize (after searching for dupes and reading similar issues for a couple hours) that there are design limitations around TS not tracking own/enumerable properties.\r\n\r\nBut, from what I understand, it makes a guess and correctly omits methods on classes from the inferred type of spreading an instance of that class. Since getters on classes are also on the prototype, it seems like inconsistent behavior that _methods_ are omitted from the inferred type but get/set accessors are not. Even though the compiler doesn’t _really_ track what properties are own enumerable, it seems like it could use similar heuristics to determine, in this simple case, that the getter won’t be present on the spread object.\r\n\r\nP.S. – thanks for your time, and apologies in advance if this _does_ turn out to be a dupe that I couldn’t find 😬 ","author":{"url":"https://github.com/andrewbranch","@type":"Person","name":"andrewbranch"},"datePublished":"2018-08-07T22:27:30Z","url":"https://github.com/microsoft/TypeScript/issues/26279"}
| 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:82270595-748b-fd10-b798-26a681815222 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 9B1E:1EC9BC:2A6603E:361106A:6B01B000 |
| html-safe-nonce | 502fa5514eee295d663a462403778e993140223c0af406852259254318109c61 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5QjFFOjFFQzlCQzoyQTY2MDNFOjM2MTEwNkE6NkIwMUIwMDAiLCJ2aXNpdG9yX2lkIjoiNTgyMTI4Mjc4MzY5MTEyNDczNiIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 8ce159f893412f3e96de5b49d8d07c0fa2fe22a3da6c74416d5b479159f6fa42 |
| hovercard-subject-tag | issue:348509428 |
| 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/26279/issue_layout |
| twitter:image | https://opengraph.githubassets.com/e6c385b91056350b2f706ada02c58c31fe462c2b6a8246166be0fb12d879545e/microsoft/TypeScript/issues/26279 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/e6c385b91056350b2f706ada02c58c31fe462c2b6a8246166be0fb12d879545e/microsoft/TypeScript/issues/26279 |
| og:image:alt | TypeScript Version: 3.1.0-dev.20180807 Search Terms: object spread own enumerable object spread accessor getter object spread Code (playground) class Phone { get number() { return 1234; } getNumber... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | andrewbranch |
| 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