Title: Function composition challenge for type system · Issue #10247 · microsoft/TypeScript · GitHub
Open Graph Title: Function composition challenge for type system · Issue #10247 · microsoft/TypeScript
X Title: Function composition challenge for type system · Issue #10247 · microsoft/TypeScript
Description: How would you type the following JavaScript function? function wrap(fn) { return (a, b) => fn(a, b); } Obviously, the naive typed declaration would be: type Func = (a: A, b: B) => R; declare function wrap(fn: Func
Open Graph Description: How would you type the following JavaScript function? function wrap(fn) { return (a, b) => fn(a, b); } Obviously, the naive typed declaration would be: type Func = (a: A, b: B) => R; decla...
X Description: How would you type the following JavaScript function? function wrap(fn) { return (a, b) => fn(a, b); } Obviously, the naive typed declaration would be: type Func<A, B, R> = (a: A, b: B) =&...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/10247
X: @github
Domain: github.com
Hey, it has json ld scripts:
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Function composition challenge for type system","articleBody":"How would you type the following JavaScript function?\n\n``` ts\nfunction wrap(fn) { return (a, b) =\u003e fn(a, b); }\n```\n\nObviously, the naive typed declaration would be:\n\n``` ts\ntype Func\u003cA, B, R\u003e = (a: A, b: B) =\u003e R;\ndeclare function wrap\u003cA, B, R\u003e(fn: Func\u003cA, B, R\u003e): Func\u003cA, B, R\u003e;\n```\n\nBut it does not really describe its contract. So, let's say if I pass generic function, which is absolutely acceptable, it's not gonna work:\n\n``` ts\nconst f1 = wrap(\u003cT\u003e(a: T, b: T): T =\u003e a || b);\n```\n\nIt's clear that `f1` must be `\u003cT\u003e(a: T, b: T) =\u003e T`, but it's inferred to be `(a: {}, b: {}) =\u003e {}`.\nAnd that's not about type inferring, because we wouldn't be able to express that contract at all.\nThe challenge here is that I want to be able to capture/propagate not only the generic parameters themselves, but also their relations. To illustrate, if I add an overload to capture that kind of signatures:\n\n``` ts\ndeclare function wrap\u003cA, B, R\u003e(fn: \u003cT\u003e(a: T, b: T) =\u003e T): \u003cT\u003e(a: T, b: T) =\u003e T;\n```\n\nthen, I still wouldn't be able to express\n\n``` ts\nconst f2 = wrap(\u003cT\u003e(a: T, b: T): [T, T] =\u003e [a, b]);\n```\n\nBecause then I would need to add overloads of unbounded number of type compositions, such as `[T]`, `T | number`, `[[T \u0026 string, number], T] | Whatever` ... infinite number of variations.\n\nHypothetically, I could express with something like this:\n\n``` ts\ndeclare function wrap\u003cF extends Function\u003e(fn: F): F;\n```\n\nbut it doesn't solve the problem either:\n- `F` isn't constrained to be requiring at least 2 arguments\n - `F extends (a: {}, b: {}) =\u003e {}` would work, but doesn't currently, because it collapses `F` to `(a: {}, b: {}) =\u003e {}`\n- it doesn't allow to express modified signature components of `F`; see an example below\n\nSo then we come to more complicated things:\n\n``` ts\nconst wrap2 = fn =\u003e args =\u003e [fn(args[0], args[1])];\n// so that\nconst f3 = wrap2((a: number, b: string): number|string =\u003e a || b); // (args: [number, string]) =\u003e [number|string]\n```\n\nHow to express that in the type system?\n\nA reader can think now that is very synthetic examples that unlikely be found in real world.\nActually, it came to me when I tried to properly type Redux store enhancers. Redux's store enhancers are powerful and built based on function compositions. Enhancers can be very generic or can be applicable to specific types of dispatchers, states etc etc. And more importantly they can be constructed being detached from specific store. If the issue falls to discussion I will provide more details of why it's required there.\n\nSo where's the proposal?\nI've been thinking of that for awhile and haven't came out with something viable yet.\nHowever, this is something we can start with:\n\n``` ts\ndeclare function wrap2\u003c~G, Ʀ\u003cT1, T2, R, ~G\u003e\u003e(fn: \u003c...G\u003e(a: T1, b: T2) =\u003e R): \u003c...G\u003e(args: [T1, T2]) =\u003e [R]);\n```\n\nOf course, ignoring the syntax, here's what was introduced:\n1. `~G` is generic set of unbinded (no such word?) generic parameters with their constraints. Since it's not the same as generic type parameter, I've marked it with `~`. So than it's applied as `\u003c...G\u003e` that means that set becomes set of generic parameters. For example `~G=\u003cT, R extends T\u003e`, so then `\u003c...G\u003e=\u003cT, R extends T\u003e`.\n2. `Ʀ\u003cT1, T2, R, ~G\u003e` (_maybe syntax `Ʀ\u003cT1, T2, R, ...G\u003e` would make more sense, btw_) is a relation between `T1`, `T2`, `R`, `~G`. It is another kind of generic information. It could be a set of relations, such as `T1=number`, `T2=string`, `R=T1|T2=number|string`. Important here, is that relations can introduce new names that can be used as generic parameters, and also, they can reference existing type parameters from enclosing generic info.\n\nProbably, examples could help to understand what I'm trying to say:\n\n``` ts\n// JavaScript\nconst f(f1, f2) =\u003e a =\u003e b =\u003e f2(f1(a), b);\n// TypeScript\ndeclare function f\u003c~G1, ~G2, Ʀ\u003c~G1, ~G2, A, B, R1, R2\u003e\u003e(\n f1: \u003c...G1\u003e(a: A) =\u003e R1,\n f2: \u003c...G2\u003e(r1: R1, b: B) =\u003e R2): \u003c...G1\u003e(a: A) =\u003e \u003c...G2\u003e(b: B) =\u003e R2;\n\n// using\nconst g = f(\n /* f1 = */ \u003cT\u003e(a: [T, T]): [T, T] =\u003e [a[1], a[0]],\n /* f2 = */ \u003cT, B extends T\u003e(r1: [T, T], b: B[]): B[] =\u003e [...r1, ...b]);\n// Inferred generic data (all can be set explicitly, syntax is pending):\n// ~G1 = \u003cT\u003e\n// ~G2 = \u003cT, B extends T\u003e\n// Ʀ\u003c~G1, ~G2, A, B, R1, R2\u003e -\u003e\n// A is [G1.T, G1.T],\n// R1 is [G1.T, G1.T],\n// R1 is [G2.T, G2.T],\n// B is G2.B[],\n// R2 is G2.B[]\n// So the return type, type of const g would be\n// \u003c...G1\u003e(a: A) =\u003e \u003c...G2\u003e(b: B) =\u003e R2 -\u003e\n// \u003cT\u003e(a: [T, T]) =\u003e \u003cG2_T extends T, B extends G2_T\u003e(b: B) =\u003e B[]\n```\n\nSimpler example from the beginning:\n\n``` ts\n// JavaScript\nfunction wrap(fn) { return (a, b) =\u003e fn(a, b); }\n// TypeScript\ndeclare function wrap\u003c~G, R\u003c~G, A, B, C\u003e\u003e(\n fn: \u003c...G\u003e(a: A, b: B) =\u003e C): \u003c...G\u003e(a: A, b: B) =\u003e C;\n\n// using\nconst f1 = wrap(\u003cT\u003e(a: T, b: T): T =\u003e a || b);\n// is the same as explicitly\nconst f1: \u003cT\u003e(a: T, b: T) =\u003e T =\n wrap\u003c\n /* ~G = */ ~\u003cT\u003e,\n \u003c~\u003cT\u003e, A, B, C\u003e -\u003e [\n A is T,\n B is T,\n C is T]\n \u003e(\u003cT\u003e(a: T, b: T): T =\u003e a || b);\n```\n\nOk, guys, what do you think of this?\n","author":{"url":"https://github.com/Igorbek","@type":"Person","name":"Igorbek"},"datePublished":"2016-08-10T07:07:38.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":28},"url":"https://github.com/10247/TypeScript/issues/10247"}
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:4614af90-ba70-5a3d-3185-3c616fbcbe31 current-catalog-service-hash 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 request-id B204:EAAB3:3AC90C:539FCE:6A61C956 html-safe-nonce 4cc35923a7558934a72bb0e2b49250304dd32ac557e4a44144d88a9a5699745b visitor-payload eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCMjA0OkVBQUIzOjNBQzkwQzo1MzlGQ0U6NkE2MUM5NTYiLCJ2aXNpdG9yX2lkIjoiNzQ5ODY3NjY2MzY3NDMzMDQ2IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= visitor-hmac 4d5313ac67a50af4849e13a838a811d0fe07549a7cdbf9d428744d36873728ce hovercard-subject-tag issue:170344580 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/10247/issue_layout twitter:image https://opengraph.githubassets.com/9331ee0d0a997dc0d3135a68750d891ea374f316d735057afb76cb3021d08a64/microsoft/TypeScript/issues/10247 twitter:card summary_large_image og:image https://opengraph.githubassets.com/9331ee0d0a997dc0d3135a68750d891ea374f316d735057afb76cb3021d08a64/microsoft/TypeScript/issues/10247 og:image:alt How would you type the following JavaScript function? function wrap(fn) { return (a, b) => fn(a, b); } Obviously, the naive typed declaration would be: type Func = (a: A, b: B) => R; decla... og:image:width 1200 og:image:height 600 og:site_name GitHub og:type object og:author:username Igorbek hostname github.com expected-hostname github.com None 6f4633bcf01c1ad14b73fd07dd39ac31d61f3d3c2578ee08ec1792b7b351eeb9 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 ac296ae7f21856f1f92adbad22f870b6fbb4b907 ui-target full theme-color #1e2327 color-scheme light dark
Links:
Viewport: width=device-width