René's URL Explorer Experiment


Title: Function parameters and return type inference with infer keyword · Issue #38182 · microsoft/TypeScript · GitHub

Open Graph Title: Function parameters and return type inference with infer keyword · Issue #38182 · microsoft/TypeScript

X Title: Function parameters and return type inference with infer keyword · Issue #38182 · microsoft/TypeScript

Description: I saw a couple of similar issues, but it all provided very poor specification of the problem, so I decided to open my own issue. Description The further step in bringing nice fp declarations into TypeScript is adding function parameters ...

Open Graph Description: I saw a couple of similar issues, but it all provided very poor specification of the problem, so I decided to open my own issue. Description The further step in bringing nice fp declarations into T...

X Description: I saw a couple of similar issues, but it all provided very poor specification of the problem, so I decided to open my own issue. Description The further step in bringing nice fp declarations into T...

Opengraph URL: https://github.com/microsoft/TypeScript/issues/38182

X: @github

direct link

Domain: github.com


Hey, it has json ld scripts:
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Function parameters and return type inference with infer keyword","articleBody":"I saw a couple of similar issues, but it all provided very poor specification of the problem, so I decided to open my own issue.\r\n\r\n# Description\r\nThe further step in bringing nice fp declarations into TypeScript is adding function parameters and return type inference through `infer` keyword.\r\n\r\nLets look at some examples:\r\n\r\n```typescript\r\ntype F1 = \u003cT extends number>(a: T, b: T) => T\r\n\r\n// Now: [number, number]\r\n// Propose: [1, 1]\r\ntype R1 = F1 extends ((a: 1, b: infer P) => infer R) ? [P, R] : never\r\n```\r\n\r\nHere we passed parameter `a` as `1` and expect:\r\n1. Type parameter `T` to be inferred as `1`\r\n2. Type `P` to be inferred as `T` -> `1`\r\n3. Type `R` to be inferred as `T` -> `1`  \r\n4. Result to be inferred as `[P, R]` -> `[1, 1]` \r\n\r\n---\r\n\r\n```typescript\r\ntype F2 = \u003cT1 extends AnyTuple, T2>(a: T1, b: T2, c: Reverse\u003cT1>) => [T1, T2]  \r\n\r\n// Now: [never, unknown, [AnyTuple, unknown]]\r\n// Propose: [[3, 2, 1], unknown, [[1, 2, 3], unknown]]\r\ntype R2 = F2 extends (a: [1, 2, 3], b: infer P2, c: infer P1) => infer R ? [P1, P2, R] : never\r\n```\r\n\r\nHere we passed parameter `a` as `[1, 2, 3]` and expect:\r\n1. Type parameter `T1` to be inferred as `[1, 2, 3]`\r\n2. Type parameter `T2` to be replaced with `unknown` type because we provided no info to be able to infer it \r\n3. Type `P2` to be inferred as `T2` -> `unknown` \r\n4. Type `P1` to be inferred as `Reverse\u003cT1>` -> `Reverse\u003c[1, 2, 3]>` -> `[3, 2, 1]`\r\n5. Type `R` to be inferred as `[T1, T2]` -> `[[1, 2, 3], unknown]`\r\n6. Result to be inferred as `[P1, P2, R]` -> `[[3, 2, 1], unknown, [[1, 2, 3], unknown]]\r\n\r\n---\r\n\r\n```typescript\r\ntype F3 = \u003cT extends AnyTuple>(a: T, b: T['length']) => typeof b\r\n\r\n// Now: never\r\n// Propose: 3\r\ntype R3 = F3 extends ((a: [1, 2, 3], b: infer LEN) => any) ? LEN : never\r\n```\r\n\r\nHere we expect:\r\n1. Type parameter `T` to be inferred as `[1, 2, 3]`\r\n2. Type LEN to be inferred as `T['length']` -> `[1, 2, 3]['length']` -> `3`\r\n3. Result to be inferred as `LEN` -> `3`\r\n\r\n---\r\n\r\nCase of [wrapping type checkers](https://dev.to/awerlogus/why-you-check-the-types-wrong-wrapping-type-checkers-ke):\r\n\r\n```typescript\r\nenum string_literal { string_literal = '' }\r\n\r\ntype StringLiteral\u003cT> = T extends string ? string extends T ? string_literal : T : string_literal\r\n\r\ntype F4 = \u003cT>(a: StringLiteral\u003cT>, b: T) => T extends '' ? [] : T\r\n\r\n// Now: all cases never\r\n\r\n// Proposed: ['foobar', 'foobar']\r\ntype R4_1 = F4 extends ((a: 'foobar', b: infer P) => infer R) ? [P, R] : never\r\n// Proposed: ['', []]\r\ntype R4_2 = F4 extends ((a: '', b: infer P) => infer R) ? [P, R] : never\r\n// Proposed: [string_literal, string]\r\ntype R4_3 = F4 extends ((a: string, b: infer P) => infer R) ? [P, R] : never\r\n```\r\n\r\n---\r\n\r\n# Use cases\r\n\r\nIn other similar issues I saw only use cases of function parameters inference, so I'll extend it with return type inference cases.\r\n\r\nThe first example is a `call` function:\r\n\r\n```typescript\r\ndeclare function call\u003cP extends AnyTuple, F extends ((...args: P) => unknown)>(\r\n  params: P, func: F\r\n): F extends (...args: P) => infer R ? R : never\r\n\r\n// Got: [unknown, unknown]\r\n// Expected: [1, 2]\r\nconst a = call([1, 2], \u003cA, B>(a: A, b: B): [A, B] => [a, b])\r\n```\r\n\r\nAlso it may be used to infer return types on each step of computing `pipe` and `flow` function versions with unlimited parameters count. I'm sure, there're much more cases where this feature may be applied, but I can't remember any more right now.\r\n\r\n# Util types used\r\n\r\n```typescript\r\ntype Empty = readonly []\r\n\r\n\r\ntype AnyTuple = ReadonlyArray\u003cunknown> & { readonly 0: unknown } | Empty\r\n\r\n\r\ntype Tail\u003cT extends AnyTuple> = ((...args: T) => any) extends (head: any, ...tail: infer R) => any\r\n\r\n  ? R extends AnyTuple ? Readonly\u003cR> : never : Empty\r\n\r\n\r\ntype Prepend\u003cT extends AnyTuple, E> = ((e: E, ...args: T) => any) extends\r\n\r\n  (...args: infer R) => any ? R extends AnyTuple ? Readonly\u003cR> : never : never\r\n\r\n\r\ntype _ReverseWith\u003cT extends AnyTuple, Result extends AnyTuple> = {\r\n  0: _ReverseWith\u003cTail\u003cT>, Prepend\u003cResult, T[0]>>\r\n  1: Readonly\u003cResult>\r\n}[T extends Empty ? 1 : 0]\r\n\r\n\r\ntype Reverse\u003cT extends AnyTuple> = _ReverseWith\u003cT, Empty>\r\n```\r\n","author":{"url":"https://github.com/awerlogus","@type":"Person","name":"awerlogus"},"datePublished":"2020-04-25T15:16:10Z","url":"https://github.com/microsoft/TypeScript/issues/38182"}

react-profiling0
react-import-mapreact,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-controllervoltron_issues_fragments
route-actionissue_layout
fetch-noncev2:9f783d2c-fe65-c08b-312f-028a4cdab993
current-catalog-service-hash81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114
request-idA960:311640:215CC6E:2A64074:6B0177FF
html-safe-noncefff8f6e83ef1a80b99c13acbb33f1c1fe34e6b6263607581143d1b02a6e0ca78
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBOTYwOjMxMTY0MDoyMTVDQzZFOjJBNjQwNzQ6NkIwMTc3RkYiLCJ2aXNpdG9yX2lkIjoiMzY2NzE5MjQyMzcyMjg3NDg3OSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9
visitor-hmacc99bb3941d236744a51e288a0742ab57eb9b2c9ffe9bc548033532601cd10b18
hovercard-subject-tagissue:606790691
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/38182/issue_layout
twitter:imagehttps://opengraph.githubassets.com/fc00390be95fb6ab4516ec6767f79b3d4428a63b81e169f37d0e342a553789f4/microsoft/TypeScript/issues/38182
twitter:cardsummary_large_image
og:imagehttps://opengraph.githubassets.com/fc00390be95fb6ab4516ec6767f79b3d4428a63b81e169f37d0e342a553789f4/microsoft/TypeScript/issues/38182
og:image:altI saw a couple of similar issues, but it all provided very poor specification of the problem, so I decided to open my own issue. Description The further step in bringing nice fp declarations into T...
og:image:width1200
og:image:height600
og:site_nameGitHub
og:typeobject
og:author:usernameawerlogus
hostnamegithub.com
expected-hostnamegithub.com
Nonecc5dfdd32f62ebd3223cab3af3aa2b268d3605218237326a48a99d1efd89495c
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
release8c77c9e8439031ad54c75a250694842892f693e1
ui-targetfull
theme-color#1e2327
color-schemelight dark
react-app-nameissues

Links:

Skip to contenthttps://github.com/microsoft/TypeScript/issues/38182#start-of-content
https://github.com/
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fmicrosoft%2FTypeScript%2Fissues%2F38182
GitHubRepository hosting, code review, and collaborationhttps://github.com/platform
CopilotAI coding agent for tasks, projects, and automated workhttps://github.com/features/copilot
ActionsCI/CD and automationhttps://github.com/features/actions
Code QualityQuality analysis, fixes, and enforcementhttps://github.com/features/code-quality
CodespacesCloud development environmentshttps://github.com/features/codespaces
Code SecurityVulnerability detection and remediationhttps://github.com/security/advanced-security/code-security
Secret ProtectionSecret scanning and leak preventionhttps://github.com/security/advanced-security/secret-protection
All Featureshttps://github.com/features
Documentationhttps://docs.github.com/en
Traininghttps://learn.github.com/skills
Certificationshttps://learn.github.com/credentials
Build with Copilothttps://github.com/features/ai
Customize Copilothttps://docs.github.com/en/copilot/how-tos/copilot-on-github/customize-copilot/customize-copilot-overview
Integrationshttps://github.com/marketplace
Trending reposhttps://github.com/trending
GitHub Sponsorshttps://github.com/open-source/sponsors
Developer articleshttps://github.com/resources/articles
ChangelogYour source for the latest features, improvements, and fixes across GitHub products, all in one place.https://github.blog/changelog/
Enterpriseshttps://github.com/enterprise
Teamhttps://github.com/team
Startupshttps://github.com/enterprise/startups
Nonprofitshttps://github.com/solutions/industry/nonprofits
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
See all industrieshttps://github.com/solutions/industry
Why GitHubhttps://github.com/why-github
Customer Storieshttps://github.com/customer-stories
Business insightshttps://github.com/solutions/executive-insights
Trust Centerhttps://github.com/trust-center
GitHub Advanced SecurityEnterprise-grade code securityhttps://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
Documentationhttps://docs.github.com/en
Resource centerhttps://github.com/resources
Bloghttps://github.blog/
Events & webinarshttps://github.com/resources/events
Community Forumhttps://github.com/orgs/community/discussions
Customer Storieshttps://github.com/customer-stories
Integrationshttps://github.com/marketplace
Customer supporthttps://help.github.com/
Traininghttps://learn.github.com/skills
GitHub Statushttps://www.githubstatus.com/
Abouthttps://github.com/about
Partnershttps://github.com/partners
Changeloghttps://github.blog/changelog/
GitHub UniverseThe event for developers, by developers — talks, workshops, and what’s next for GitHub.https://githubuniverse.com/
Pricinghttps://github.com/pricing
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fmicrosoft%2FTypeScript%2Fissues%2F38182
Sign uphttps://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/38182
Reloadhttps://github.com/microsoft/TypeScript/issues/38182
Reloadhttps://github.com/microsoft/TypeScript/issues/38182
microsofthttps://github.com/microsoft
TypeScripthttps://github.com/microsoft/TypeScript
Codehttps://github.com/microsoft/TypeScript
Issues5k (5k)https://github.com/microsoft/TypeScript/issues
Pull requests312 (312)https://github.com/microsoft/TypeScript/pulls
Actionshttps://github.com/microsoft/TypeScript/actions
Projectshttps://github.com/microsoft/TypeScript/projects
Wikihttps://github.com/microsoft/TypeScript/wiki
Security and qualityhttps://github.com/microsoft/TypeScript/security
Insightshttps://github.com/microsoft/TypeScript/insights
https://github.com/microsoft/TypeScript
New issuehttps://github.com/login?return_to=https://github.com/microsoft/TypeScript/issues/38182
New issuehttps://github.com/login?return_to=https://github.com/microsoft/TypeScript/issues/38182
Function parameters and return type inference with infer keywordhttps://github.com/microsoft/TypeScript/issues/38182#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/awerlogus
Michael Borislav (awerlogus)https://github.com/awerlogus
on Apr 25, 2020https://github.com/microsoft/TypeScript/issues/38182#issue-606790691
wrapping type checkershttps://dev.to/awerlogus/why-you-check-the-types-wrong-wrapping-type-checkers-ke
https://github.com/mscottnelson
Michael Scott-Nelson (mscottnelson)https://github.com/mscottnelson
on May 3, 2020https://github.com/microsoft/TypeScript/issues/38182#issuecomment-623186548
Ryan Cavanaugh (RyanCavanaugh)https://github.com/RyanCavanaugh
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
on May 11, 2020https://github.com/microsoft/TypeScript/issues/38182#event-3323577159
https://github.com/RyanCavanaugh
Ryan Cavanaugh (RyanCavanaugh)https://github.com/RyanCavanaugh
on May 11, 2020https://github.com/microsoft/TypeScript/issues/38182#issuecomment-626783809
Anders Hejlsberg (@ahejlsberg)https://github.com/ahejlsberg
Jonghyeon Ko (manudeli)https://github.com/manudeli
on Jun 13, 2024https://github.com/microsoft/TypeScript/issues/38182#event-13139890805
fix(react-query): improved TypeScript engine halt due to return type …https://github.com/toss/suspensive/commit/c6c06400e0792d488a4284c0caeefe306f99288e
c6c0640https://github.com/toss/suspensive/commit/c6c06400e0792d488a4284c0caeefe306f99288e
Jonghyeon Ko (manudeli)https://github.com/manudeli
on Aug 3, 2024https://github.com/microsoft/TypeScript/issues/38182#event-13751100933
fix(react-query): improved TypeScript engine halt due to return type …https://github.com/toss/suspensive/commit/3b8f5dcdd74709524ef666f6c2ed7208ab01b3c7
3b8f5dchttps://github.com/toss/suspensive/commit/3b8f5dcdd74709524ef666f6c2ed7208ab01b3c7
fix(react-query): improved TypeScript engine halt due to return type …https://github.com/toss/suspensive/commit/68753b2eb9f75117bc915fbb6aa5bf178a1cb734
68753b2https://github.com/toss/suspensive/commit/68753b2eb9f75117bc915fbb6aa5bf178a1cb734
fix(react-query): improved TypeScript engine halt due to return type …https://github.com/toss/suspensive/commit/da5db5f419a0af299e0d0ba013f02c023fdd3368
da5db5fhttps://github.com/toss/suspensive/commit/da5db5f419a0af299e0d0ba013f02c023fdd3368
fix(react-query): improved TypeScript engine halt due to return type …https://github.com/toss/suspensive/commit/4c04ca36bcb475815521661c2e0f589baf543576
4c04ca3https://github.com/toss/suspensive/commit/4c04ca36bcb475815521661c2e0f589baf543576
Sign up for freehttps://github.com/signup?return_to=https://github.com/microsoft/TypeScript/issues/38182
Sign in to commenthttps://github.com/login?return_to=https://github.com/microsoft/TypeScript/issues/38182
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.