René's URL Explorer Experiment


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

direct link

Domain: github.com


Hey, it has json ld scripts:
{"@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-profiling0
react-import-mapreact,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-controllervoltron_issues_fragments
route-actionissue_layout
fetch-noncev2:b0e35865-07d1-1be4-2117-1a7c83529587
current-catalog-service-hash81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114
request-idBADE:37820D:117381A:173B987:6B010ABE
html-safe-nonce03e08af368053da721ec7147fb9f98cedc6f43ffd887de635311e40102daeb7e
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCQURFOjM3ODIwRDoxMTczODFBOjE3M0I5ODc6NkIwMTBBQkUiLCJ2aXNpdG9yX2lkIjoiODAwMTcwMjY1NDY4ODQzMDc4MiIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9
visitor-hmacb9615c115b5a73a9715735da95543f5ff93f63f178e39971c647f3af06ef8918
hovercard-subject-tagissue:301671660
github-keyboard-shortcutsrepository,issues,copilot
google-site-verificationApib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I
octolytics-urlhttps://collector.github.com/github/collect
analytics-location///voltron/issues_fragments/issue_layout
fb:app_id1401488693436528
apple-itunes-appapp-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/microsoft/TypeScript/22285/issue_layout
twitter:imagehttps://opengraph.githubassets.com/3a7a7de8f3090ebd701b389bc5285e0db6559101ca1885f7fbc2cf139575b848/microsoft/TypeScript/issues/22285
twitter:cardsummary_large_image
og:imagehttps://opengraph.githubassets.com/3a7a7de8f3090ebd701b389bc5285e0db6559101ca1885f7fbc2cf139575b848/microsoft/TypeScript/issues/22285
og:image:altI have created this playground that shows the problem. The code looks like this: export interface Extender { instance: (constructor: { new() }, methods?: {}) =>...
og:image:width1200
og:image:height600
og:site_nameGitHub
og:typeobject
og:author:usernamedagda1
hostnamegithub.com
expected-hostnamegithub.com
Nonecb7fa109eca7d4156c889f474306b0e91336055e9adf8fd4684b61e49094d124
turbo-cache-controlno-preview
go-importgithub.com/microsoft/TypeScript git https://github.com/microsoft/TypeScript.git
octolytics-dimension-user_id6154722
octolytics-dimension-user_loginmicrosoft
octolytics-dimension-repository_id20929025
octolytics-dimension-repository_nwomicrosoft/TypeScript
octolytics-dimension-repository_publictrue
octolytics-dimension-repository_is_forkfalse
octolytics-dimension-repository_network_root_id20929025
octolytics-dimension-repository_network_root_nwomicrosoft/TypeScript
turbo-body-classeslogged-out env-production page-responsive
disable-turbofalse
browser-stats-urlhttps://api.github.com/_private/browser/stats
browser-errors-urlhttps://api.github.com/_private/browser/errors
release2b59d31a4e44f08b7af67a4b67f8fd0af3befe2a
ui-targetfull
theme-color#1e2327
color-schemelight dark
react-app-nameissues

Links:

Skip to contenthttps://github.com/microsoft/TypeScript/issues/22285#start-of-content
https://github.com/
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fmicrosoft%2FTypeScript%2Fissues%2F22285
GitHubRepository hosting, code review, and collaborationhttps://github.com/platform
CopilotAI coding agent for tasks, projects, and automated workhttps://github.com/features/copilot
ActionsCI/CD and automationhttps://github.com/features/actions
Code QualityQuality analysis, fixes, and enforcementhttps://github.com/features/code-quality
CodespacesCloud development environmentshttps://github.com/features/codespaces
Code SecurityVulnerability detection and remediationhttps://github.com/security/advanced-security/code-security
Secret ProtectionSecret scanning and leak preventionhttps://github.com/security/advanced-security/secret-protection
All Featureshttps://github.com/features
Documentationhttps://docs.github.com/en
Traininghttps://learn.github.com/skills
Certificationshttps://learn.github.com/credentials
Build with Copilothttps://github.com/features/ai
Customize Copilothttps://docs.github.com/en/copilot/how-tos/copilot-on-github/customize-copilot/customize-copilot-overview
Integrationshttps://github.com/marketplace
Trending reposhttps://github.com/trending
GitHub Sponsorshttps://github.com/open-source/sponsors
Developer articleshttps://github.com/resources/articles
ChangelogYour source for the latest features, improvements, and fixes across GitHub products, all in one place.https://github.blog/changelog/
Enterpriseshttps://github.com/enterprise
Teamhttps://github.com/team
Startupshttps://github.com/enterprise/startups
Nonprofitshttps://github.com/solutions/industry/nonprofits
Healthcarehttps://github.com/solutions/industry/healthcare
Financial serviceshttps://github.com/solutions/industry/financial-services
Manufacturinghttps://github.com/solutions/industry/manufacturing
Governmenthttps://github.com/solutions/industry/government
See all industrieshttps://github.com/solutions/industry
Why GitHubhttps://github.com/why-github
Customer Storieshttps://github.com/customer-stories
Business insightshttps://github.com/solutions/executive-insights
Trust Centerhttps://github.com/trust-center
GitHub Advanced SecurityEnterprise-grade code securityhttps://github.com/security/advanced-security
Copilot for businessEnterprise-grade AI featureshttps://github.com/features/copilot/copilot-business
Premium SupportEnterprise-grade 24/7 supporthttps://github.com/enterprise/premium-support
Documentationhttps://docs.github.com/en
Resource centerhttps://github.com/resources
Bloghttps://github.blog/
Events & webinarshttps://github.com/resources/events
Community Forumhttps://github.com/orgs/community/discussions
Customer Storieshttps://github.com/customer-stories
Integrationshttps://github.com/marketplace
Customer supporthttps://help.github.com/
Traininghttps://learn.github.com/skills
GitHub Statushttps://www.githubstatus.com/
Abouthttps://github.com/about
Partnershttps://github.com/partners
Changeloghttps://github.blog/changelog/
GitHub UniverseThe event for developers, by developers — talks, workshops, and what’s next for GitHub.https://githubuniverse.com/
Pricinghttps://github.com/pricing
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fmicrosoft%2FTypeScript%2Fissues%2F22285
Sign uphttps://github.com/signup?ref_cta=Sign+up&ref_loc=header+logged+out&ref_page=%2F%3Cuser-name%3E%2F%3Crepo-name%3E%2Fvoltron%2Fissues_fragments%2Fissue_layout&source=header-repo&source_repo=microsoft%2FTypeScript
Reloadhttps://github.com/microsoft/TypeScript/issues/22285
Reloadhttps://github.com/microsoft/TypeScript/issues/22285
Reloadhttps://github.com/microsoft/TypeScript/issues/22285
microsofthttps://github.com/microsoft
TypeScripthttps://github.com/microsoft/TypeScript
Codehttps://github.com/microsoft/TypeScript
Issues5k (5k)https://github.com/microsoft/TypeScript/issues
Pull requests310 (310)https://github.com/microsoft/TypeScript/pulls
Actionshttps://github.com/microsoft/TypeScript/actions
Projectshttps://github.com/microsoft/TypeScript/projects
Wikihttps://github.com/microsoft/TypeScript/wiki
Security and qualityhttps://github.com/microsoft/TypeScript/security
Insightshttps://github.com/microsoft/TypeScript/insights
https://github.com/microsoft/TypeScript
New issuehttps://github.com/login?return_to=https://github.com/microsoft/TypeScript/issues/22285
New issuehttps://github.com/login?return_to=https://github.com/microsoft/TypeScript/issues/22285
How to handle this when this is a functionhttps://github.com/microsoft/TypeScript/issues/22285#top
Needs More InfoThe issue still hasn't been fully clarifiedhttps://github.com/microsoft/TypeScript/issues?q=state%3Aopen%20label%3A%22Needs%20More%20Info%22
https://github.com/dagda1
Paul (dagda1)https://github.com/dagda1
on Mar 2, 2018https://github.com/microsoft/TypeScript/issues/22285#issue-301671660
playgroundhttps://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
Needs More InfoThe issue still hasn't been fully clarifiedhttps://github.com/microsoft/TypeScript/issues?q=state%3Aopen%20label%3A%22Needs%20More%20Info%22
on Mar 2, 2018https://github.com/microsoft/TypeScript/issues/22285#event-1501447546
https://github.com/dagda1
Paul (dagda1)https://github.com/dagda1
on Mar 2, 2018https://github.com/microsoft/TypeScript/issues/22285#issuecomment-370055669
Andy (Andrewkraft) (@Andy-MS)https://github.com/Andy-MS
jsfiddlehttps://jsfiddle.net/dagda1/1zm60eL6/27/
https://github.com/dagda1
Paul (dagda1)https://github.com/dagda1
on Mar 3, 2018https://github.com/microsoft/TypeScript/issues/22285#issuecomment-370126777
https://github.com/dagda1
Paul (dagda1)https://github.com/dagda1
on Mar 3, 2018https://github.com/microsoft/TypeScript/issues/22285#issuecomment-370173616
Andy (Andrewkraft) (@Andy-MS)https://github.com/Andy-MS
Paul (dagda1)https://github.com/dagda1
completedhttps://github.com/microsoft/TypeScript/issues?q=is%3Aissue%20state%3Aclosed%20archived%3Afalse%20reason%3Acompleted
on Mar 4, 2018https://github.com/microsoft/TypeScript/issues/22285#event-1503077756
https://github.com/dagda1
Paul (dagda1)https://github.com/dagda1
on Mar 5, 2018https://github.com/microsoft/TypeScript/issues/22285#issuecomment-370534671
Andy (Andrewkraft) (@Andy-MS)https://github.com/Andy-MS
microsofthttps://github.com/microsoft
on Jul 25, 2018https://github.com/microsoft/TypeScript/issues/22285#event-1753421137
Sign up for freehttps://github.com/signup?return_to=https://github.com/microsoft/TypeScript/issues/22285
Sign in to commenthttps://github.com/login?return_to=https://github.com/microsoft/TypeScript/issues/22285
Needs More InfoThe issue still hasn't been fully clarifiedhttps://github.com/microsoft/TypeScript/issues?q=state%3Aopen%20label%3A%22Needs%20More%20Info%22
https://github.com
Termshttps://docs.github.com/site-policy/github-terms/github-terms-of-service
Privacyhttps://docs.github.com/site-policy/privacy-policies/github-privacy-statement
Securityhttps://github.com/security
Statushttps://www.githubstatus.com/
Communityhttps://github.community/
Docshttps://docs.github.com/
Contacthttps://support.github.com?tags=dotcom-footer

Viewport: width=device-width


URLs of crawlers that visited me.