Title: A syntax for returning "this" for method call chaining would be great · Issue #7620 · microsoft/TypeScript · GitHub
Open Graph Title: A syntax for returning "this" for method call chaining would be great · Issue #7620 · microsoft/TypeScript
X Title: A syntax for returning "this" for method call chaining would be great · Issue #7620 · microsoft/TypeScript
Description: When you've got a bit of inheritance going on, there can be limits to what you can do with method chaining. Say you've got these two classes: class Animal { public sleep() : Animal { return this; } } class Elephant extends Animal { publi...
Open Graph Description: When you've got a bit of inheritance going on, there can be limits to what you can do with method chaining. Say you've got these two classes: class Animal { public sleep() : Animal { return this; }...
X Description: When you've got a bit of inheritance going on, there can be limits to what you can do with method chaining. Say you've got these two classes: class Animal { public sleep() : Animal { return...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/7620
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"A syntax for returning \"this\" for method call chaining would be great","articleBody":"When you've got a bit of inheritance going on, there can be limits to what you can do with method chaining. Say you've got these two classes:\n\n```\nclass Animal {\n public sleep() : Animal {\n return this;\n }\n}\n\nclass Elephant extends Animal {\n public squirtWithTrunk(): Elephant {\n return this;\n }\n}\n```\n\nGiven an instance of Elephant, you could do this:\nmyElephant.squirtWithTrunk().sleep()\n\nbut you couldn't do this:\nmyElephant.sleep().squirtWithTrunk()\n\nBecause Animal doen't have a \"squirtWithTrunk\" method.\n\nSeems fair enough, but given how much code space method chaining can save, and how much that can matter in JavaScript, it's a shame. And obviously in raw JavaScript this sort of thing just works.\n\nIf the following syntax were allowed, and \"this\" could be passed through a method chain without its type being narrowed to ancestor types, I think that might be quite nice:\n\n```\nclass Animal {\n public sleep() : this {\n }\n}\n\nclass Elephant extends Animal {\n public squirtWithTrunk(): this {\n }\n}\n```\n\nI don't know how feasible that would be from a compiler point of view, but as a user of the language I'd find it very useful. In the last code snippet I imagine that the return statement would be disallowed, and that the returned value would always be the current object.\n","author":{"url":"https://github.com/philthy256","@type":"Person","name":"philthy256"},"datePublished":"2016-03-21T20:55:50Z","url":"https://github.com/microsoft/TypeScript/issues/7620"}
| 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:32f1b246-511f-1219-a7ca-980dd59f960f |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | AD94:11EACE:816805:AED1D1:6B00E0F4 |
| html-safe-nonce | 580a4e774b423a3335aaf33674dfcf9557858b6160537154db6c225bab9d4e38 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBRDk0OjExRUFDRTo4MTY4MDU6QUVEMUQxOjZCMDBFMEY0IiwidmlzaXRvcl9pZCI6Ijc4OTAzNjMyODk3NjEyNzYxNDgiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 00cf4d653a1ca726a619d8c4a94101a15644e8de014c7a00782de9fa8003e23f |
| hovercard-subject-tag | issue:142468809 |
| 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/7620/issue_layout |
| twitter:image | https://opengraph.githubassets.com/4603e4c3ef7509765edfc860ebc7acb3741a476c391dae2f5b8743eb8c7ab8ed/microsoft/TypeScript/issues/7620 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/4603e4c3ef7509765edfc860ebc7acb3741a476c391dae2f5b8743eb8c7ab8ed/microsoft/TypeScript/issues/7620 |
| og:image:alt | When you've got a bit of inheritance going on, there can be limits to what you can do with method chaining. Say you've got these two classes: class Animal { public sleep() : Animal { return this; }... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | philthy256 |
| hostname | github.com |
| expected-hostname | github.com |
| None | 043c3f30fbcbe2c6c8e729e10cbbbc90c406546f9fbbb67c648d38f6b7d16daf |
| 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 | 5425dc24e8bd8d6fca9641a12afc984825a43875 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width