Title: Improve type inference for generic curried functions · Issue #15016 · microsoft/TypeScript · GitHub
Open Graph Title: Improve type inference for generic curried functions · Issue #15016 · microsoft/TypeScript
X Title: Improve type inference for generic curried functions · Issue #15016 · microsoft/TypeScript
Description: // #-------------- This works type Repeat = (n: number) => (s: string) => string; export const repeat: Repeat = n => s => s.repeat(n); // Sweet! s and n are both inferred! // -------------------------# // #----- Problems begin here type ...
Open Graph Description: // #-------------- This works type Repeat = (n: number) => (s: string) => string; export const repeat: Repeat = n => s => s.repeat(n); // Sweet! s and n are both inferred! // ----------------------...
X Description: // #-------------- This works type Repeat = (n: number) => (s: string) => string; export const repeat: Repeat = n => s => s.repeat(n); // Sweet! s and n are both inferred! // ----------...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/15016
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Improve type inference for generic curried functions","articleBody":"```ts\r\n// #-------------- This works\r\n\r\ntype Repeat = (n: number) =\u003e (s: string) =\u003e string;\r\nexport const repeat: Repeat = n =\u003e s =\u003e s.repeat(n); // Sweet! s and n are both inferred!\r\n\r\n// -------------------------#\r\n\r\n\r\n// #----- Problems begin here\r\n\r\ntype Repeat2 = (n: number) =\u003e \u003cT extends { repeat(n: number): T }\u003e(repeatable: T) =\u003e T;\r\n\r\n// Only n is inferred as number. Won't work with noImplicitAny enabled\r\nexport const repeat2Broken: Repeat2 = n =\u003e r =\u003e r.repeat(n); \r\n// Workaround is to redeclare everything from the type signature\r\nexport const repeat2Ugly: Repeat2 =\r\n n =\u003e \u003cT extends { repeat(n: number): T }\u003e(r: T) =\u003e r.repeat(n);\r\n\r\n// -------------------------#\r\n\r\n\r\n// #----------- It gets worse\r\n\r\n// Type inference breaks for all args following the generic one, so even if you use\r\n// the workaround, the next args won't regain inference\r\ntype Repeat3 = \r\n (n: number) \r\n =\u003e \u003cT extends { repeat(n: number): T }\u003e(repeatable: T) \r\n =\u003e (reverse: boolean) \r\n =\u003e T;\r\n\r\n// Even if you explicitly provide the boilerplate for the generic arg, the boolean arg \r\n// won't be inferred. So the following won't work with noImplicitAny enabled\r\nexport const repeat3UglyAndBroken: Repeat3 =\r\n n =\u003e \u003cT extends { repeat(n: number): T }\u003e(r: T) =\u003e b =\u003e { ... }\r\n\r\n// You have to add typing for the last arg\r\nexport const repeat3Uglier: Repeat3 =\r\n n =\u003e \u003cT extends { repeat(n: number): T }\u003e(r: T) =\u003e (b: boolean) =\u003e { ... }\r\n\r\n// -------------------------#\r\n```\r\n\r\nI'd really appreciate better support for good inference in this kind of scenario, whether it is solved by supporting partial generic application or addition of participation of return-types in inference or whatever. \r\n\r\nJS libraries that use a functional programming paradigm often end up loosely or any-ly typed, because you find yourself fighting the type system and adding repetitive type noise all over your code to derive any safety. A couple of examples where better support for FP would be useful:\r\n\r\n - Libraries like React or Apollo make use of curried higher-order functions for solving cross-cutting concerns. In-fact, my original use case comes from React higher-order components\r\n - Popular FP libraries like Ramda become tedious to use in a `noImplicitAny` project because any generic functions passed or received from the library tend to quickly devolve into `{}` unless you repeatedly hand-hold the type system\r\n\r\nHere is my original use-case, if interested:\r\n\r\n```ts\r\nexport type GetState\u003cTState\u003e = () =\u003e TState;\r\nexport type SetState\u003cTState\u003e = (newState: TState) =\u003e void;\r\nexport type StatelessComponent\u003cTProp\u003e = (prop: TProp) =\u003e React.ReactElement\u003cTProp\u003e\r\nexport type StatefulComponent\u003cTProp, TState\u003e = \r\n (stateAccess: { getState: GetState\u003cTState\u003e, setState: SetState\u003cTState\u003e })\r\n =\u003e StatelessComponent\u003cTProp\u003e;\r\n\r\ntype ScrollableHOC = \r\n (options: ScrollableOptions) \r\n =\u003e \u003cT extends ScrollableProps\u003e(Child: ReactComponent\u003cT\u003e) \r\n =\u003e StatefulComponent\u003cT, ScrollableState\u003e;\r\n\r\n// The introduction of the Child arg breaks type inference for getState, setState, props\r\nconst scrollable: ScrollableHOC = opts =\u003e Child =\u003e ({ getState, setState }) =\u003e props =\u003e {\r\n // ...\r\n}\r\n```","author":{"url":"https://github.com/masaeedu","@type":"Person","name":"masaeedu"},"datePublished":"2017-04-05T01:12:47.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":25},"url":"https://github.com/15016/TypeScript/issues/15016"}
| 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:af635299-6dff-7cda-50b8-2fc85f2bdc7e |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | B31C:9B7C5:4CBF91:68C3F1:6A629B86 |
| html-safe-nonce | 586d686dc80a1fb5097e6e2276b9be51939b80fd440e18f4250ac167115e59a3 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCMzFDOjlCN0M1OjRDQkY5MTo2OEMzRjE6NkE2MjlCODYiLCJ2aXNpdG9yX2lkIjoiNzM2MzUxNDA0ODI4MDk1OTg3OCIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 62390a019d1bdc0929194aae84481bdf24fe7898f0d75defec0a28b495662c31 |
| hovercard-subject-tag | issue:219433823 |
| 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/15016/issue_layout |
| twitter:image | https://opengraph.githubassets.com/d1484e360c1d9d73311bc4d8f63a9305861dc37292b8c1908fff0123c53e8c42/microsoft/TypeScript/issues/15016 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/d1484e360c1d9d73311bc4d8f63a9305861dc37292b8c1908fff0123c53e8c42/microsoft/TypeScript/issues/15016 |
| og:image:alt | // #-------------- This works type Repeat = (n: number) => (s: string) => string; export const repeat: Repeat = n => s => s.repeat(n); // Sweet! s and n are both inferred! // ----------------------... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | masaeedu |
| hostname | github.com |
| expected-hostname | github.com |
| None | a6443033349a18134b517627f043d4a59a5110337b879810c7229c2c7e28eda0 |
| 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 | d6c6cacbe32d3ddfd1796fbd1a39b8fb47b87daf |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width