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
Domain: github.com
{"@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-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:6e656025-0aa0-214d-fd3f-43c20e949827 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 9C80:CC1A4:2E50EDC:3AEB358:6B01BBB9 |
| html-safe-nonce | b8cda910edc0739e3369427c58e33598ef0badace4f07b8aa23725aafde23850 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5QzgwOkNDMUE0OjJFNTBFREM6M0FFQjM1ODo2QjAxQkJCOSIsInZpc2l0b3JfaWQiOiIzMDMwMTIyNjYwNjAxMzc5NzY5IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 065c415af0f018b6b4be1e2155ed4b0a2ec67b1e0571736f29aba472b404058b |
| hovercard-subject-tag | issue:574492204 |
| 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/37181/issue_layout |
| twitter:image | https://opengraph.githubassets.com/48c55c9e6f593db7565c6d3d4488ff1148d97ebf720b5040ee850b754c81811e/microsoft/TypeScript/issues/37181 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/48c55c9e6f593db7565c6d3d4488ff1148d97ebf720b5040ee850b754c81811e/microsoft/TypeScript/issues/37181 |
| og:image:alt | 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... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | mperktold |
| 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