René's URL Explorer Experiment


Title: Allow binding generic functions to a given type · Issue #37181 · microsoft/TypeScript · GitHub

Open Graph Title: Allow binding generic functions to a given type · Issue #37181 · microsoft/TypeScript

X Title: Allow binding generic functions to a given type · Issue #37181 · microsoft/TypeScript

Description: Search Terms generic function, parametric function, function type parameter, function type argument The problem is closely related to #26043, but the proposed solution is different. Suggestion Currently, there is no way of explicitly pro...

Open Graph Description: Search Terms generic function, parametric function, function type parameter, function type argument The problem is closely related to #26043, but the proposed solution is different. Suggestion Curr...

X Description: Search Terms generic function, parametric function, function type parameter, function type argument The problem is closely related to #26043, but the proposed solution is different. Suggestion Curr...

Opengraph URL: https://github.com/microsoft/TypeScript/issues/37181

X: @github

direct link

Domain: github.com


Hey, it has json ld scripts:
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Allow binding generic functions to a given type","articleBody":"## Search Terms\r\ngeneric function, parametric function, function type parameter, function type argument\r\n\r\nThe problem is closely related to #26043, but the proposed solution is different.\r\n\r\n## Suggestion\r\n\r\nCurrently, there is no way of explicitly providing type arguments to generic functions. For example, consider the following function:\r\n\r\n```ts\r\nfunction box\u003cT>(value: T) {\r\n  return { value };\r\n}\r\n```\r\n\r\nTypeScript correctly infers the return type `{ value: T }`. However, it's not possible to derive the return type for a particular `T`, say `number`, at least not without ugly workarounds such as creating new functions just to fix the type arguments:\r\n\r\n```ts\r\ntype Box = ReturnType\u003ctypeof box>;  // { value: unknown }\r\ntype Box\u003cT> = ReturnType\u003ctypeof ???>;  // can't propagate T from Box to box\r\n\r\nconst numBox = (n: number) => box(n);  // ugly workaround\r\ntype NumBox = ReturnType\u003ctypeof numBox>;\r\n```\r\n\r\nSo I suggest to provide a syntax that allows to do just that: fix type arguments of generic functions:\r\n\r\n```ts\r\ntype Box\u003cT> = ReturnType\u003ctypeof box\u003cT>>;\r\ntype NumBox = ReturnType\u003ctypeof box\u003cnumber>>;\r\n```\r\n\r\nThis syntax resembles the existing one for providing type arguments when _calling_ a function:\r\n\r\n```ts\r\nconst b = box\u003cnumber>(2);\r\n```\r\n\r\nWith this proposal implemented, interpreting such a call could be split into two parts:\r\nFirst fix the type argument of box to `number`, which yields a function that takes a number.\r\nSecond, apply that function to `2`.\r\nHence, it could also be written with parenthesis: `(box\u003cnumber>)(2)`.\r\nI wouldn't suggest doing that in practice, but it shows that this proposal increases composability of generic functions.\r\n\r\n## Use Cases\r\n\r\nI tried to come up with a way to implement discriminated unions more succintly. The idea is to start with factory functions and then combine the return types:\r\n\r\n```ts\r\nfunction val\u003cV>(value: V) {\r\n    return { type: \"Val\", value } as const;\r\n}\r\nfunction plus\u003cV>(left: V, right: V) {\r\n    return { type: \"Plus\", left, right } as const;\r\n}\r\nfunction times\u003cV>(left: V, right: V) {\r\n    return { type: \"Times\", left, right } as const;\r\n}\r\n\r\ntype Expr\u003cV> = ReturnType\u003ctypeof val\u003cV>>\r\n             | ReturnType\u003ctypeof plus\u003cV>>\r\n             | ReturnType\u003ctypeof times\u003cV>>;\r\n```\r\n\r\nWith this approach, you would get convenient factory functions for building instances, without duplicating the object structure.\r\n\r\nBut it doesn't work, because it relies on the new syntax I just proposed.\r\n\r\n## Checklist\r\n\r\nMy suggestion meets these guidelines:\r\n\r\n* [x] This wouldn't be a breaking change in existing TypeScript/JavaScript code\r\n* [x] This wouldn't change the runtime behavior of existing JavaScript code\r\n* [x] This could be implemented without emitting different JS based on the types of the expressions\r\n* [x] This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)\r\n* [x] This feature would agree with the rest of [TypeScript's Design Goals](https://github.com/Microsoft/TypeScript/wiki/TypeScript-Design-Goals).\r\n","author":{"url":"https://github.com/mperktold","@type":"Person","name":"mperktold"},"datePublished":"2020-03-03T08:16:52Z","url":"https://github.com/microsoft/TypeScript/issues/37181"}

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:6e656025-0aa0-214d-fd3f-43c20e949827
current-catalog-service-hash81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114
request-id9C80:CC1A4:2E50EDC:3AEB358:6B01BBB9
html-safe-nonceb8cda910edc0739e3369427c58e33598ef0badace4f07b8aa23725aafde23850
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5QzgwOkNDMUE0OjJFNTBFREM6M0FFQjM1ODo2QjAxQkJCOSIsInZpc2l0b3JfaWQiOiIzMDMwMTIyNjYwNjAxMzc5NzY5IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0=
visitor-hmac065c415af0f018b6b4be1e2155ed4b0a2ec67b1e0571736f29aba472b404058b
hovercard-subject-tagissue:574492204
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/37181/issue_layout
twitter:imagehttps://opengraph.githubassets.com/48c55c9e6f593db7565c6d3d4488ff1148d97ebf720b5040ee850b754c81811e/microsoft/TypeScript/issues/37181
twitter:cardsummary_large_image
og:imagehttps://opengraph.githubassets.com/48c55c9e6f593db7565c6d3d4488ff1148d97ebf720b5040ee850b754c81811e/microsoft/TypeScript/issues/37181
og:image:altSearch Terms generic function, parametric function, function type parameter, function type argument The problem is closely related to #26043, but the proposed solution is different. Suggestion Curr...
og:image:width1200
og:image:height600
og:site_nameGitHub
og:typeobject
og:author:usernamemperktold
hostnamegithub.com
expected-hostnamegithub.com
Nonecc5dfdd32f62ebd3223cab3af3aa2b268d3605218237326a48a99d1efd89495c
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
release8c77c9e8439031ad54c75a250694842892f693e1
ui-targetfull
theme-color#1e2327
color-schemelight dark
react-app-nameissues

Links:

Skip to contenthttps://github.com/microsoft/TypeScript/issues/37181#start-of-content
https://github.com/
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fmicrosoft%2FTypeScript%2Fissues%2F37181
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%2F37181
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/37181
Reloadhttps://github.com/microsoft/TypeScript/issues/37181
Reloadhttps://github.com/microsoft/TypeScript/issues/37181
microsofthttps://github.com/microsoft
TypeScripthttps://github.com/microsoft/TypeScript
Codehttps://github.com/microsoft/TypeScript
Issues5k (5k)https://github.com/microsoft/TypeScript/issues
Pull requests312 (312)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/37181
New issuehttps://github.com/login?return_to=https://github.com/microsoft/TypeScript/issues/37181
#47607https://github.com/microsoft/TypeScript/pull/47607
Allow binding generic functions to a given typehttps://github.com/microsoft/TypeScript/issues/37181#top
#47607https://github.com/microsoft/TypeScript/pull/47607
CommittedThe team has roadmapped this issuehttps://github.com/microsoft/TypeScript/issues?q=state%3Aopen%20label%3A%22Committed%22
SuggestionAn idea for TypeScripthttps://github.com/microsoft/TypeScript/issues?q=state%3Aopen%20label%3A%22Suggestion%22
https://github.com/mperktold
Matthias Perktold (mperktold)https://github.com/mperktold
on Mar 3, 2020https://github.com/microsoft/TypeScript/issues/37181#issue-574492204
#26043https://github.com/microsoft/TypeScript/issues/26043
TypeScript's Design Goalshttps://github.com/Microsoft/TypeScript/wiki/TypeScript-Design-Goals
Nexthttps://github.com/microsoft/TypeScript/issues/37181?timeline_page=1
Ryan Cavanaugh (RyanCavanaugh)https://github.com/RyanCavanaugh
In DiscussionNot yet reached consensushttps://github.com/microsoft/TypeScript/issues?q=state%3Aopen%20label%3A%22In%20Discussion%22
SuggestionAn idea for TypeScripthttps://github.com/microsoft/TypeScript/issues?q=state%3Aopen%20label%3A%22Suggestion%22
on Mar 4, 2020https://github.com/microsoft/TypeScript/issues/37181#event-3098630634
https://github.com/RyanCavanaugh
Ryan Cavanaugh (RyanCavanaugh)https://github.com/RyanCavanaugh
on Mar 4, 2020https://github.com/microsoft/TypeScript/issues/37181#issuecomment-594860387
#6606https://github.com/microsoft/TypeScript/issues/6606
https://github.com/mperktold
Matthias Perktold (mperktold)https://github.com/mperktold
on Mar 7, 2020https://github.com/microsoft/TypeScript/issues/37181#issuecomment-596059982
#6606https://github.com/microsoft/TypeScript/issues/6606
#6606https://github.com/microsoft/TypeScript/issues/6606
https://github.com/SimonMeskens
Simon Meskens (SimonMeskens)https://github.com/SimonMeskens
on Mar 20, 2020https://github.com/microsoft/TypeScript/issues/37181#issuecomment-601861734
Ryan Cavanaugh (@RyanCavanaugh)https://github.com/RyanCavanaugh
https://github.com/SimonMeskens
Simon Meskens (SimonMeskens)https://github.com/SimonMeskens
on Mar 20, 2020https://github.com/microsoft/TypeScript/issues/37181#issuecomment-601870635
https://github.com/bradennapier
Braden Napier (bradennapier)https://github.com/bradennapier
on Apr 8, 2020https://github.com/microsoft/TypeScript/issues/37181#issuecomment-610704352
Simon Meskens (@SimonMeskens)https://github.com/SimonMeskens
#37835https://github.com/microsoft/TypeScript/issues/37835
https://github.com/SimonMeskens
Simon Meskens (SimonMeskens)https://github.com/SimonMeskens
on Apr 8, 2020https://github.com/microsoft/TypeScript/issues/37181#issuecomment-610706336
https://github.com/bradennapier
Braden Napier (bradennapier)https://github.com/bradennapier
on Apr 8, 2020https://github.com/microsoft/TypeScript/issues/37181#issuecomment-610706955
https://github.com/SimonMeskens
Simon Meskens (SimonMeskens)https://github.com/SimonMeskens
on Apr 8, 2020https://github.com/microsoft/TypeScript/issues/37181#issuecomment-610712396
https://github.com/bradennapier
Braden Napier (bradennapier)https://github.com/bradennapier
on Apr 8, 2020https://github.com/microsoft/TypeScript/issues/37181#issuecomment-610712667
https://github.com/Harpush
Harpushhttps://github.com/Harpush
on Jun 5, 2020https://github.com/microsoft/TypeScript/issues/37181#issuecomment-639389379
https://github.com/Jack-Works
Jack Works (Jack-Works)https://github.com/Jack-Works
on Jun 23, 2020https://github.com/microsoft/TypeScript/issues/37181#issuecomment-648181745
https://github.com/mperktold
Matthias Perktold (mperktold)https://github.com/mperktold
on Jun 23, 2020https://github.com/microsoft/TypeScript/issues/37181#issuecomment-648217279
Jack Works (@Jack-Works)https://github.com/Jack-Works
https://github.com/mperktold
Matthias Perktold (mperktold)https://github.com/mperktold
on Jun 23, 2020https://github.com/microsoft/TypeScript/issues/37181#issuecomment-648267689
Braden Napier (@bradennapier)https://github.com/bradennapier
https://github.com/Harpush
Harpushhttps://github.com/Harpush
on Jun 15, 2021https://github.com/microsoft/TypeScript/issues/37181#issuecomment-861876344
Ryan Cavanaugh (@RyanCavanaugh)https://github.com/RyanCavanaugh
https://github.com/mperktold
Matthias Perktold (mperktold)https://github.com/mperktold
on Jun 16, 2021https://github.com/microsoft/TypeScript/issues/37181#issuecomment-862426451
https://github.com/DavideValdo
Davide Biagini (DavideValdo)https://github.com/DavideValdo
on Jun 16, 2021https://github.com/microsoft/TypeScript/issues/37181#issuecomment-862441181
https://github.com/mperktold
Matthias Perktold (mperktold)https://github.com/mperktold
on Jun 16, 2021https://github.com/microsoft/TypeScript/issues/37181#issuecomment-862476991
Davide Biagini (@DavideValdo)https://github.com/DavideValdo
Braden Napier (@bradennapier)https://github.com/bradennapier
https://github.com/sandorvasas
sandorvasas (sandorvasas)https://github.com/sandorvasas
on Jun 16, 2021https://github.com/microsoft/TypeScript/issues/37181#issuecomment-862533862
https://github.com/tavindev
Gustavo (tavindev)https://github.com/tavindev
on Jul 12, 2021https://github.com/microsoft/TypeScript/issues/37181#issuecomment-878481552
https://github.com/deviprsd
Devi Prasad (deviprsd)https://github.com/deviprsd
on Sep 17, 2021https://github.com/microsoft/TypeScript/issues/37181#issuecomment-921430745
https://github.com/SlavikSurminskiy
Vyacheslav Surminskiy (SlavikSurminskiy)https://github.com/SlavikSurminskiy
on Sep 30, 2021https://github.com/microsoft/TypeScript/issues/37181#issuecomment-931236852
https://github.com/SlavikSurminskiy
Vyacheslav Surminskiy (SlavikSurminskiy)https://github.com/SlavikSurminskiy
on Sep 30, 2021https://github.com/microsoft/TypeScript/issues/37181#issuecomment-931237070
https://github.com/alamothe
alamothehttps://github.com/alamothe
on Jan 27, 2022https://github.com/microsoft/TypeScript/issues/37181#issuecomment-1022836228
https://stackoverflow.com/questions/50321419/typescript-returntype-of-generic-functionhttps://stackoverflow.com/questions/50321419/typescript-returntype-of-generic-function
https://github.com/ahejlsberg
Anders Hejlsberg (ahejlsberg)https://github.com/ahejlsberg
on Jan 27, 2022https://github.com/microsoft/TypeScript/issues/37181#issuecomment-1023267858
#47607https://github.com/microsoft/TypeScript/pull/47607
Anders Hejlsberg (ahejlsberg)https://github.com/ahejlsberg
completedhttps://github.com/microsoft/TypeScript/issues?q=is%3Aissue%20state%3Aclosed%20archived%3Afalse%20reason%3Acompleted
#47607https://github.com/microsoft/TypeScript/pull/47607
on Feb 16, 2022https://github.com/microsoft/TypeScript/issues/37181#event-6085945105
https://github.com/mwiemer-microsoft
Mark Wiemer (MSFT) (mwiemer-microsoft)https://github.com/mwiemer-microsoft
on May 6, 2022https://github.com/microsoft/TypeScript/issues/37181#issuecomment-1119843349
Ryan Cavanaugh (RyanCavanaugh)https://github.com/RyanCavanaugh
CommittedThe team has roadmapped this issuehttps://github.com/microsoft/TypeScript/issues?q=state%3Aopen%20label%3A%22Committed%22
In DiscussionNot yet reached consensushttps://github.com/microsoft/TypeScript/issues?q=state%3Aopen%20label%3A%22In%20Discussion%22
on May 6, 2022https://github.com/microsoft/TypeScript/issues/37181#event-6565283758
Sign up for freehttps://github.com/signup?return_to=https://github.com/microsoft/TypeScript/issues/37181
Sign in to commenthttps://github.com/login?return_to=https://github.com/microsoft/TypeScript/issues/37181
CommittedThe team has roadmapped this issuehttps://github.com/microsoft/TypeScript/issues?q=state%3Aopen%20label%3A%22Committed%22
SuggestionAn idea for TypeScripthttps://github.com/microsoft/TypeScript/issues?q=state%3Aopen%20label%3A%22Suggestion%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.