René's URL Explorer Experiment


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

direct link

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-controllervoltron_issues_fragments
route-actionissue_layout
fetch-noncev2:4614af90-ba70-5a3d-3185-3c616fbcbe31
current-catalog-service-hash81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114
request-idB204:EAAB3:3AC90C:539FCE:6A61C956
html-safe-nonce4cc35923a7558934a72bb0e2b49250304dd32ac557e4a44144d88a9a5699745b
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCMjA0OkVBQUIzOjNBQzkwQzo1MzlGQ0U6NkE2MUM5NTYiLCJ2aXNpdG9yX2lkIjoiNzQ5ODY3NjY2MzY3NDMzMDQ2IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0=
visitor-hmac4d5313ac67a50af4849e13a838a811d0fe07549a7cdbf9d428744d36873728ce
hovercard-subject-tagissue:170344580
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/10247/issue_layout
twitter:imagehttps://opengraph.githubassets.com/9331ee0d0a997dc0d3135a68750d891ea374f316d735057afb76cb3021d08a64/microsoft/TypeScript/issues/10247
twitter:cardsummary_large_image
og:imagehttps://opengraph.githubassets.com/9331ee0d0a997dc0d3135a68750d891ea374f316d735057afb76cb3021d08a64/microsoft/TypeScript/issues/10247
og:image:altHow 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:width1200
og:image:height600
og:site_nameGitHub
og:typeobject
og:author:usernameIgorbek
hostnamegithub.com
expected-hostnamegithub.com
None6f4633bcf01c1ad14b73fd07dd39ac31d61f3d3c2578ee08ec1792b7b351eeb9
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
releaseac296ae7f21856f1f92adbad22f870b6fbb4b907
ui-targetfull
theme-color#1e2327
color-schemelight dark

Links:

Skip to contenthttps://github.com/microsoft/TypeScript/issues/10247#start-of-content
https://github.com/
Sign in https://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fmicrosoft%2FTypeScript%2Fissues%2F10247
GitHub CopilotWrite better code with AIhttps://github.com/features/copilot
GitHub Copilot appDirect agents from issue to mergehttps://github.com/features/ai/github-app
MCP RegistryNewIntegrate external toolshttps://github.com/mcp
ActionsAutomate any workflowhttps://github.com/features/actions
CodespacesInstant dev environmentshttps://github.com/features/codespaces
IssuesPlan and track workhttps://github.com/features/issues
Code ReviewManage code changeshttps://github.com/features/code-review
Code QualityEnforce quality at mergehttps://github.com/features/code-quality
GitHub Advanced SecurityFind and fix vulnerabilitieshttps://github.com/security/advanced-security
Code securitySecure your code as you buildhttps://github.com/security/advanced-security/code-security
Secret protectionStop leaks before they starthttps://github.com/security/advanced-security/secret-protection
Why GitHubhttps://github.com/why-github
Documentationhttps://docs.github.com
Bloghttps://github.blog
Changeloghttps://github.blog/changelog
Marketplacehttps://github.com/marketplace
View all featureshttps://github.com/features
Enterpriseshttps://github.com/enterprise
Small and medium teamshttps://github.com/team
Startupshttps://github.com/enterprise/startups
Nonprofitshttps://github.com/solutions/industry/nonprofits
App Modernizationhttps://github.com/solutions/use-case/app-modernization
DevSecOpshttps://github.com/solutions/use-case/devsecops
DevOpshttps://github.com/solutions/use-case/devops
CI/CDhttps://github.com/solutions/use-case/ci-cd
View all use caseshttps://github.com/solutions/use-case
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
View all industrieshttps://github.com/solutions/industry
View all solutionshttps://github.com/solutions
AIhttps://github.com/resources/articles?topic=ai
Software Developmenthttps://github.com/resources/articles?topic=software-development
DevOpshttps://github.com/resources/articles?topic=devops
Securityhttps://github.com/resources/articles?topic=security
View all topicshttps://github.com/resources/articles
Customer storieshttps://github.com/customer-stories
Events & webinarshttps://github.com/resources/events
Ebooks & reportshttps://github.com/resources/whitepapers
Business insightshttps://github.com/solutions/executive-insights
GitHub Skillshttps://skills.github.com
Documentationhttps://docs.github.com
Customer supporthttps://support.github.com
Community forumhttps://github.com/orgs/community/discussions
Trust centerhttps://github.com/trust-center
Partnershttps://github.com/partners
View all resourceshttps://github.com/resources
GitHub SponsorsFund open source developershttps://github.com/open-source/sponsors
Security Labhttps://securitylab.github.com
Maintainer Communityhttps://maintainers.github.com
Acceleratorhttps://github.com/open-source/accelerator
GitHub Starshttps://stars.github.com
Archive Programhttps://archiveprogram.github.com
Topicshttps://github.com/topics
Trendinghttps://github.com/trending
Collectionshttps://github.com/collections
Enterprise platformAI-powered developer platformhttps://github.com/enterprise
GitHub Advanced SecurityEnterprise-grade security featureshttps://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
Pricinghttps://github.com/pricing
Search syntax tipshttps://docs.github.com/search-github/github-code-search/understanding-github-code-search-syntax
documentationhttps://docs.github.com/search-github/github-code-search/understanding-github-code-search-syntax
Sign in https://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fmicrosoft%2FTypeScript%2Fissues%2F10247
Sign up https://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/10247
Reloadhttps://github.com/microsoft/TypeScript/issues/10247
Reloadhttps://github.com/microsoft/TypeScript/issues/10247
Please reload this pagehttps://github.com/microsoft/TypeScript/issues/10247
microsoft https://github.com/microsoft
TypeScripthttps://github.com/microsoft/TypeScript
Notifications https://github.com/login?return_to=%2Fmicrosoft%2FTypeScript
Fork 13.6k https://github.com/login?return_to=%2Fmicrosoft%2FTypeScript
Star 110k https://github.com/login?return_to=%2Fmicrosoft%2FTypeScript
Code https://github.com/microsoft/TypeScript
Issues 5k+ https://github.com/microsoft/TypeScript/issues
Pull requests 21 https://github.com/microsoft/TypeScript/pulls
Actions https://github.com/microsoft/TypeScript/actions
Projects https://github.com/microsoft/TypeScript/projects
Models https://github.com/microsoft/TypeScript/models
Wiki https://github.com/microsoft/TypeScript/wiki
Security and quality 0 https://github.com/microsoft/TypeScript/security
Insights https://github.com/microsoft/TypeScript/pulse
Code https://github.com/microsoft/TypeScript
Issues https://github.com/microsoft/TypeScript/issues
Pull requests https://github.com/microsoft/TypeScript/pulls
Actions https://github.com/microsoft/TypeScript/actions
Projects https://github.com/microsoft/TypeScript/projects
Models https://github.com/microsoft/TypeScript/models
Wiki https://github.com/microsoft/TypeScript/wiki
Security and quality https://github.com/microsoft/TypeScript/security
Insights https://github.com/microsoft/TypeScript/pulse
Function composition challenge for type systemhttps://github.com/microsoft/TypeScript/issues/10247#top
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
https://github.com/Igorbek
Igorbekhttps://github.com/Igorbek
on Aug 10, 2016https://github.com/microsoft/TypeScript/issues/10247#issue-170344580
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
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.