Title: Smarter type inference for functional programming (RxJS, Ramda, etc) · Issue #15680 · microsoft/TypeScript · GitHub
Open Graph Title: Smarter type inference for functional programming (RxJS, Ramda, etc) · Issue #15680 · microsoft/TypeScript
X Title: Smarter type inference for functional programming (RxJS, Ramda, etc) · Issue #15680 · microsoft/TypeScript
Description: EDIT: updated the types in example to match what works perfectly in Flow type TypeScript Version: 2.2.1 / nightly (2.2.0-dev.201xxxxx) TypeScript is unable to infer types through higher-order functions. Code Given some imaginary class Se...
Open Graph Description: EDIT: updated the types in example to match what works perfectly in Flow type TypeScript Version: 2.2.1 / nightly (2.2.0-dev.201xxxxx) TypeScript is unable to infer types through higher-order funct...
X Description: EDIT: updated the types in example to match what works perfectly in Flow type TypeScript Version: 2.2.1 / nightly (2.2.0-dev.201xxxxx) TypeScript is unable to infer types through higher-order funct...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/15680
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Smarter type inference for functional programming (RxJS, Ramda, etc)","articleBody":"**EDIT: updated the types in example to match what works perfectly in Flow type**\r\n\r\n\u003c!-- BUGS: Please use this template. -->\r\n\u003c!-- QUESTIONS: This is not a general support forum! Ask Qs at http://stackoverflow.com/questions/tagged/typescript -->\r\n\u003c!-- SUGGESTIONS: See https://github.com/Microsoft/TypeScript-wiki/blob/master/Writing-Good-Design-Proposals.md -->\r\n\r\n**TypeScript Version:** 2.2.1 / nightly (2.2.0-dev.201xxxxx)\r\n\r\nTypeScript is unable to infer types through higher-order functions.\r\n\r\n**Code**\r\n\r\nGiven some imaginary class `SetOf`, which is just some set of values we want to compose operations over... We'll try the following:\r\n\r\n```ts\r\n// This is a contrived class. We could do the same thing with Observables, etc.\r\nclass SetOf\u003cA> {\r\n _store: A[];\r\n\r\n add(a: A) {\r\n this._store.push(a);\r\n }\r\n\r\n transform\u003cB>(transformer: (a: SetOf\u003cA>) => SetOf\u003cB>): SetOf\u003cB> {\r\n return transformer(this);\r\n }\r\n\r\n forEach(fn: (a: A, index: number) => void) {\r\n this._store.forEach((a, i) => fn(a, i));\r\n }\r\n}\r\n\r\nfunction compose\u003cA, B, C, D, E>(\r\n fnA: (a: SetOf\u003cA>) => SetOf\u003cB>, \r\n fnB: (b: SetOf\u003cB>) => SetOf\u003cC>, \r\n fnC: (c: SetOf\u003cC>) => SetOf\u003cD>,\r\n fnD: (c: SetOf\u003cD>) => SetOf\u003cE>,\r\n):(x: SetOf\u003cA>) => SetOf\u003cE>;\r\n/* ... etc ... */\r\nfunction compose\u003cT>(...fns: ((x: T) => T)[]): (x: T) => T {\r\n return (x: T) => fns.reduce((prev, fn) => fn(prev), x);\r\n}\r\n\r\nfunction map\u003cA, B>(fn: (a: A) => B): (s: SetOf\u003cA>) => SetOf\u003cB> {\r\n return (a: SetOf\u003cA>) => {\r\n const b: SetOf\u003cB> = new SetOf();\r\n a.forEach(x => b.add(fn(x)));\r\n return b;\r\n }\r\n}\r\n\r\nfunction filter\u003cA>(predicate: (a: A) => boolean): (s: SetOf\u003cA>) => SetOf\u003cA> {\r\n return (a: SetOf\u003cA>) => {\r\n const result = new SetOf\u003cA>();\r\n a.forEach(x => {\r\n if (predicate(x)) result.add(x);\r\n });\r\n return result;\r\n }\r\n}\r\n\r\nconst testSet = new SetOf();\r\ntestSet.add(1);\r\ntestSet.add(2);\r\ntestSet.add(3);\r\n\r\n// THE PROBLEM IS HERE\r\n// all functions below are unable to infer any types.\r\n// The user will be required to annotate the types manually at each step.\r\ntestSet.transform(\r\n compose(\r\n filter(x => x % 1 === 0),\r\n map(x => x + x),\r\n map(x => x + '!!!'),\r\n map(x => x.toUpperCase())\r\n )\r\n)\r\n\r\ntestSet.transform(\r\n compose(\r\n filter(x => x % 1 === 0),\r\n map(x => x + x),\r\n map(x => 123), // Whoops a bug\r\n map(x => x.toUpperCase()) // causes an error!\r\n )\r\n)\r\n```\r\n\r\n## Libraries Where This Problem Will Exist\r\n\r\n### RxJS\r\n\r\nWe want to move RxJS over to using \"lettable operators\". That means operators that are built in a similar fashion to what you see in the example able. The current prototype augmentation is untenable for large applications, and makes tree-shaking hard if not impossible with regards to Rx.\r\n\r\n### Ramda\r\n\r\nA widely popular functional programming library will undoubtedly have the same issues. Especially give that (I think) the `compose` function shown above exists (probably in a better form) within Ramdba.\r\n\r\n### Redux\r\n\r\n`combineReducers` in redux is liable to have this same problem, at least when dealing with typed reducers. I don't think the reducer's types can be inferred inside of that call. However, I'm not sure it's a common use case for Redux to have inline reducers in a `combineReducers` call.\r\n\r\n### Plain JavaScript\r\n\r\nThis problem would exist for any higher-order function used in this manner within JavaScript:\r\n\r\n```ts\r\n// given the same `compose` function from above:\r\n\r\nconst result = [1, 2, 3, 4].map(\r\n compose(\r\n x => x + x,\r\n x => x + '!!!',\r\n x => parseInt(x)\r\n )\r\n);\r\n\r\nconsole.log(result); // [2, 4, 6, 8]\r\n```\r\n\r\ncc/ @rkirov @igorminar @david-driscoll @alexeagle ","author":{"url":"https://github.com/benlesh","@type":"Person","name":"benlesh"},"datePublished":"2017-05-08T20:52:29Z","url":"https://github.com/microsoft/TypeScript/issues/15680"}
| 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:f212ccae-3536-764f-72fd-36b4c947ce69 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | CAD0:21E3E7:85569B:B23B3E:6B00DE4F |
| html-safe-nonce | ecf13c37618af17ca5ee9ed58bc986e3442114226010589543cf11410dca4e56 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDQUQwOjIxRTNFNzo4NTU2OUI6QjIzQjNFOjZCMDBERTRGIiwidmlzaXRvcl9pZCI6IjgzMTYyMDcyNjEzNDUxNzcxNjciLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 82339f1ada78a85da60d93cd45648b1454ff5ebd1df7d83fbcbf94029979e288 |
| hovercard-subject-tag | issue:227170857 |
| 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/15680/issue_layout |
| twitter:image | https://opengraph.githubassets.com/b6e550513713173555008fce5b87b950715f784ca79a8c9e095821e27fd95df9/microsoft/TypeScript/issues/15680 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/b6e550513713173555008fce5b87b950715f784ca79a8c9e095821e27fd95df9/microsoft/TypeScript/issues/15680 |
| og:image:alt | EDIT: updated the types in example to match what works perfectly in Flow type TypeScript Version: 2.2.1 / nightly (2.2.0-dev.201xxxxx) TypeScript is unable to infer types through higher-order funct... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | benlesh |
| hostname | github.com |
| expected-hostname | github.com |
| None | 043c3f30fbcbe2c6c8e729e10cbbbc90c406546f9fbbb67c648d38f6b7d16daf |
| 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 | 5425dc24e8bd8d6fca9641a12afc984825a43875 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width