Title: How to handle this when this is a function · Issue #22285 · microsoft/TypeScript · GitHub
Open Graph Title: How to handle this when this is a function · Issue #22285 · microsoft/TypeScript
X Title: How to handle this when this is a function · Issue #22285 · microsoft/TypeScript
Description: I have created this playground that shows the problem. The code looks like this: export interface Extender { instance: (constructor: { new() }, methods?: {}) => void; for: (value: any) => A; symbol: S...
Open Graph Description: I have created this playground that shows the problem. The code looks like this: export interface Extender { instance: (constructor: { new() }, methods?: {}) =>...
X Description: I have created this playground that shows the problem. The code looks like this: export interface Extender<A extends { new (name: string): A }> { instance: (constructor: { new() }, methods?: ...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/22285
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"How to handle this when this is a function","articleBody":"I have created this [playground](https://www.typescriptlang.org/play/#src=export%20interface%20Extender%3CA%20extends%20%7B%20new%20(name%3A%20string)%3A%20A%20%7D%3E%20%7B%0D%0A%20%20instance%3A%20(constructor%3A%20%7B%20new()%20%7D%2C%20methods%3F%3A%20%7B%7D)%20%3D%3E%20void%3B%0D%0A%20%20for%3A%20(value%3A%20any)%20%3D%3E%20A%3B%0D%0A%20%20symbol%3A%20Symbol%3B%0D%0A%7D%0D%0A%0D%0Aexport%20function%20extend%3CA%20extends%20%7B%20new(name%3A%20string)%20%7D%3E(%0D%0A%20%20Class%3A%20A%0D%0A)%3A%20A%20%26%20Extender%3CA%3E%20%7B%0D%0A%20%20const%20name%20%3D%20Class.name%3B%0D%0A%20%20const%20Extended%20%3D%20%3CA%20%26%20Extender%3CA%3E%3EClass%3B%0D%0A%0D%0A%20%20let%20symbol%20%3D%20Symbol(name)%3B%0D%0A%0D%0A%20%20Extended.instance%20%3D%20function(constructor%2C%20methods)%20%7B%0D%0A%20%20%20%20constructor.prototype%5Bsymbol%5D%20%3D%20methods%3B%0D%0A%20%20%7D%3B%0D%0A%0D%0A%20%20Extended.symbol%20%3D%20symbol%3B%0D%0A%0D%0A%20%20Extended.for%20%3D%20function%20_for(value%3A%20any)%3A%20A%20%7B%0D%0A%20%20%20%20let%20i%20%3D%20value%5Bsymbol%5D%3B%0D%0A%0D%0A%20%20%20%20return%20i%3B%0D%0A%20%20%7D%3B%20%20%0D%0A%0D%0A%20%20const%20properties%20%3D%20getOwnPropertyDescriptors(Class.prototype)%3B%0D%0A%0D%0A%20%20%20%20Object.keys(properties)%0D%0A%20%20%20%20.filter(key%20%3D%3E%20key%20!%3D%20%22constructor%22)%0D%0A%20%20%20%20.forEach(key%20%3D%3E%20%7B%0D%0A%20%20%20%20%20%20Extended.prototype%5Bkey%5D%20%3D%20Extended.prototype%5Bkey%5D.bind(Extended.for)%3B%0D%0A%20%20%20%20%7D)%3B%0D%0A%0D%0A%20%20return%20Extended%3B%0D%0A%7D%0D%0A%0D%0Ainterface%20Interface%3CA%3E%20%7B%0D%0A%20%20doSomething%3A%20(left%3A%20A%2C%20right%3A%20A)%20%3D%3E%20A%3B%0D%0A%7D%0D%0A%0D%0Aconst%20Implementation%20%3D%20extend(class%20Implementation%3CA%3E%20implements%20Interface%3CA%3E%20%7B%0D%0A%20%20doSomething(left%3A%20A%2C%20right%3A%20A)%20%7B%0D%0A%20%20%20%20let%20%7B%20doSomething%20%7D%20%3D%20this(left)%3B%0D%0A%0D%0A%20%20%20%20return%20doSomething(left%2C%20right)%3B%0D%0A%20%20%7D%0D%0A%7D)%3B%0D%0A%0D%0Aconst%20%7B%20doSomething%20%7D%20%3D%20Implementation.prototype%3B%0D%0A%0D%0AImplementation.instance(Object%2C%20%7B%0D%0A%20%20doSomething(o1%2C%20o2)%20%7B%0D%0A%20%20%20%20let%20properties%20%3D%20Object.assign(%7B%7D%2C%20propertiesOf(o1)%2C%20propertiesOf(o2))%3B%0D%0A%20%20%20%20return%20Object.create(getPrototypeOf(o1)%2C%20properties)%3B%0D%0A%20%20%7D%0D%0A%7D)%3B%0D%0A%0D%0Aconst%20n%20%3D%20doSomething(%7Bo%3A%201%7D%2C%20%7Bb%3A%202%7D)%3B) that shows the problem.\r\n\r\nThe code looks like this:\r\n\r\n```\r\nexport interface Extender\u003cA extends { new (name: string): A }> {\r\n instance: (constructor: { new() }, methods?: {}) => void;\r\n for: (value: any) => A;\r\n symbol: Symbol;\r\n}\r\n\r\nexport function extend\u003cA extends { new(name: string) }>(\r\n Class: A\r\n): A & Extender\u003cA> {\r\n const name = Class.name;\r\n const Extended = \u003cA & Extender\u003cA>>Class;\r\n\r\n let symbol = Symbol(name);\r\n\r\n Extended.instance = function(constructor, methods) {\r\n constructor.prototype[symbol] = methods;\r\n };\r\n\r\n Extended.symbol = symbol;\r\n\r\n Extended.for = function _for(value: any): A {\r\n let i = value[symbol];\r\n\r\n return i;\r\n }; \r\n\r\n const properties = getOwnPropertyDescriptors(Class.prototype);\r\n\r\n Object.keys(properties)\r\n .filter(key => key != \"constructor\")\r\n .forEach(key => {\r\n Extended.prototype[key] = Extended.prototype[key].bind(Extended.for);\r\n });\r\n\r\n return Extended;\r\n}\r\n\r\ninterface Interface\u003cA> {\r\n doSomething: (left: A, right: A) => A;\r\n}\r\n\r\nconst Implementation = extend(class Implementation\u003cA> implements Interface\u003cA> {\r\n doSomething(left: A, right: A) {\r\n let { doSomething } = this(left);\r\n\r\n return doSomething(left, right);\r\n }\r\n});\r\n\r\nconst { doSomething } = Implementation.prototype;\r\n\r\nImplementation.instance(Object, {\r\n doSomething(o1, o2) {\r\n let properties = Object.assign({}, propertiesOf(o1), propertiesOf(o2));\r\n return Object.create(getPrototypeOf(o1), properties);\r\n }\r\n});\r\n\r\ndoSomething({o: 1}, {b: 2});\r\n```\r\n\r\nHow do I handle `this` on line 44 of the playground\r\n\r\n`let { doSomething } = this(left);`\r\n\r\nthe call to `this` will return the specific `doSomething`.\r\n\r\nAt the moment I get the error, this lacks a callable signature.\r\n\r\nIf I add the false this param like this:\r\n\r\n`doSomething(this: (a: A) => Implementation\u003cA>, left: A, right: A) {`\r\n\r\nAnd if I then export the function\r\n\r\n`export {doSomething} = Implementation.prototype;`\r\n\r\nAnd I then try to use the function:\r\n\r\n`doSomething({o: 1}, {b: 2});` I get the error:\r\n\r\n> The 'this' context of type 'void' is not assignable to method's 'this' of type '(a: any) => Implementation\u003cany>'.\r\n\r\nI realise this is quite contrived but is this too contrived for typescript?","author":{"url":"https://github.com/dagda1","@type":"Person","name":"dagda1"},"datePublished":"2018-03-02T06:59:10Z","url":"https://github.com/microsoft/TypeScript/issues/22285"}
| 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:b0e35865-07d1-1be4-2117-1a7c83529587 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | BADE:37820D:117381A:173B987:6B010ABE |
| html-safe-nonce | 03e08af368053da721ec7147fb9f98cedc6f43ffd887de635311e40102daeb7e |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCQURFOjM3ODIwRDoxMTczODFBOjE3M0I5ODc6NkIwMTBBQkUiLCJ2aXNpdG9yX2lkIjoiODAwMTcwMjY1NDY4ODQzMDc4MiIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | b9615c115b5a73a9715735da95543f5ff93f63f178e39971c647f3af06ef8918 |
| hovercard-subject-tag | issue:301671660 |
| 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/22285/issue_layout |
| twitter:image | https://opengraph.githubassets.com/3a7a7de8f3090ebd701b389bc5285e0db6559101ca1885f7fbc2cf139575b848/microsoft/TypeScript/issues/22285 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/3a7a7de8f3090ebd701b389bc5285e0db6559101ca1885f7fbc2cf139575b848/microsoft/TypeScript/issues/22285 |
| og:image:alt | I have created this playground that shows the problem. The code looks like this: export interface Extender { instance: (constructor: { new() }, methods?: {}) =>... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | dagda1 |
| hostname | github.com |
| expected-hostname | github.com |
| None | cb7fa109eca7d4156c889f474306b0e91336055e9adf8fd4684b61e49094d124 |
| 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 | 2b59d31a4e44f08b7af67a4b67f8fd0af3befe2a |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width