René's URL Explorer Experiment


Title: Infer-only function generics · Issue #62182 · microsoft/TypeScript · GitHub

Open Graph Title: Infer-only function generics · Issue #62182 · microsoft/TypeScript

X Title: Infer-only function generics · Issue #62182 · microsoft/TypeScript

Description: 🔍 Search Terms generic infer generic typeof typeof arguments infer-only generics infer arguments unoverridable generics ✅ Viability Checklist This wouldn't be a breaking change in existing TypeScript/JavaScript code This wouldn't change ...

Open Graph Description: 🔍 Search Terms generic infer generic typeof typeof arguments infer-only generics infer arguments unoverridable generics ✅ Viability Checklist This wouldn't be a breaking change in existing TypeScri...

X Description: 🔍 Search Terms generic infer generic typeof typeof arguments infer-only generics infer arguments unoverridable generics ✅ Viability Checklist This wouldn't be a breaking change in existing Type...

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

X: @github

direct link

Domain: github.com


Hey, it has json ld scripts:
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Infer-only function generics","articleBody":"### 🔍 Search Terms\n\n* `generic infer`\n* `generic typeof`\n* `typeof arguments`\n* `infer-only generics`\n* `infer arguments`\n* `unoverridable generics`\n\n### ✅ Viability Checklist\n\n- [x] This wouldn't be a breaking change in existing TypeScript/JavaScript code\n- [x] This wouldn't change the runtime behavior of existing JavaScript code\n- [x] This could be implemented without emitting different JS based on the types of the expressions\n- [x] This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)\n- [x] This isn't a request to add a new utility type: https://github.com/microsoft/TypeScript/wiki/No-New-Utility-Types\n- [x] This feature would agree with the rest of our Design Goals: https://github.com/Microsoft/TypeScript/wiki/TypeScript-Design-Goals\n\n### ⭐ Suggestion\n\nAlthough there are many proposals for various syntax to allow specifying only specific, named generics (e.g. #23696) or skipping specification of certain generics (e.g. #26242), these miss a key use case: **sometimes you want to reference an inferred type that doesn't make sense to override**.\n\nI propose adding a way to reference inferred types in generics, without making them able to be manually overridden, by using the `infer` keyword in function argument type definitions.\n\n```typescript\nfunction create\u003c\n  TOutput = InferOutput\u003cTTableName>,\n  TInput = InferInput\u003cTTableName>,\n>(\n  table: infer TTableName extends string,\n  data?: TInput\n): Promise\u003cTOutput>\n```\n\nTheoretically, this could also be applied to classes:\n\n```typescript\nclass DatabaseTable {\n    constructor(public readonly name: infer TTableName extends string) {}\n\n    create\u003cTInput = InferInput\u003cTTableName>, TOutput = InferOutput\u003cTTableName>>(input: TInput): TOutput {}\n}\n```\n\nBut I'll admit that I haven't thought super deeply about the use cases or implications of that, so I'm going to focus on the function use case.\n\n### 📃 Motivating Example\n\nConsider the following generic function that automatically infers the input and output types of a SQL `CREATE` query based on the provided table name:\n\n```typescript\nasync function create\u003c\n  TTableName extends string,\n  TOutput = InferOutput\u003cTTableName>,\n  TInput = InferInput\u003cTTableName>,\n>(\n  table: TTableName,\n  data?: TInput\n): Promise\u003cTOutput> {\n  // stub\n  return {} as unknown as TOutput\n}\n```\n\nThis works as is, but it does come with a few limitations.\n\n### Limitations\n\n#### Redundant and/or Contradictory `TTableName` definitions\n\nIdeally, we want to be able to override `TOutput` and `TInput` for special cases, but overriding `TTableName` doesn't really make sense. However, we are currently _forced_ to manually specify `TTableName` because it is a required generic parameter:\n\n```typescript\n// this works, but is frustratingly redundant\ncreate\u003c'users', UserOut, UserIn>('users', {})\n```\n\nIt also leads to potentially confusing contradictions in the types,\n\n```typescript\n// Argument of type '\"friends\"' is not assignable to parameter of type '\"users\"'. ts(2345)\ncreate\u003c'users', UserOut, UserIn>('admins', {})\n```\n\n#### Inability to define a default value\n\nBecause `TTableName` is a standard generic that can be overridden, we can't define a default value for `table` in the function definition -- after all, the caller _could_ have specified a value for `TTableName`, and our default value might not agree with it!\n\n```typescript\ncreate\u003c...>(\n  // Type 'string' is not assignable to type 'TTableName'.\n  // 'string' is assignable to the constraint of type 'TTableName', but 'TTableName' could be instantiated with a different subtype of constraint 'string'. ts(2322)\n  table: TTableName = 'users',\n  data?: TInput\n)\n```\n\n### The Solution\n\nWhat we really want is a way to reference the inferred type of `table` _without_ having to define a redundant, potentially conflicting, overridable generic. This could be accomplished by allowing the `infer` keyword to be used as the type of a function argument:\n\n```typescript\nfunction create\u003c\n  TOutput = InferOutput\u003cTTableName>,\n  TInput = InferInput\u003cTTableName>,\n>(\n  table: infer TTableName extends string,\n  data?: TInput\n): Promise\u003cTOutput>\n```\n\nThis would expose the type `TTableName` in all of the same contexts that other generics could be used:\n\n* In other generics\n* In the return type\n* in subsequent arguments\n* In the function body\n\n#### Implementation challenges\n\n> _\"You know nothing, Jon Snow\"_ - Ygritte\n\nI freely admit that my knowledge of TypeScript internals is, even generously... sparse. That said, I do have some thoughts about the potential challenges of implementing this feature.\n\nFirst of all, it seems that these \"named inferred argument types\" (for lack of a better term) would most likely need to be processed _before everything else_ to allow them to be referenced everywhere, including in the (ostensibly preceding) generics. I'm not sure how feasible that is, or if the TypeScript parser would easily handle something like that.\n\n### 💻 Use Cases\n\n### What do you want to use this for?\n\nThis would be invaluable for library development in a plethora of use cases. Personally, I tend to develop custom database wrappers and query builders, every single one of which has suffered some of the limitations of the current syntax.\n\n### What shortcomings exist with current approaches?\n\n* Redundancy when you must manually specify generics which are normally inferred\n* Potentially confusing contradictions between the generics and actual arguments\n* Inability to specify default values for inferred generics\n\n### What workarounds are you using in the meantime?\n\nIn the meantime, I am sucking it up and just having the redundancy and being diligent about conflicting types. For the default values problem, there are some possible workarounds, such as defining the default in the function body:\n\n```typescript\nasync function create\u003c\n  TTableName extends string = 'users',\n  TOutput = InferOutput\u003cTTableName>,\n  TInput = InferInput\u003cTTableName>,\n>(\n  table?: TTableName,\n  data?: TInput\n): Promise\u003cTOutput> {\n  const actualTable = table || 'users'\n  // stub\n  return {} as unknown as TOutput\n}\n```\n\nThis technically works, but comes with its own limitations.","author":{"url":"https://github.com/rintaun","@type":"Person","name":"rintaun"},"datePublished":"2025-08-03T08:58:26Z","url":"https://github.com/microsoft/TypeScript/issues/62182"}

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:116b89ca-136f-d7f7-0667-f3d91286d223
current-catalog-service-hash81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114
request-id8FF4:76F02:338BF09:41F2096:6B01D8C5
html-safe-noncefd244048beb7426633366cb020c2bf2f96f219347fce897264be68da7d9a1cb3
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4RkY0Ojc2RjAyOjMzOEJGMDk6NDFGMjA5Njo2QjAxRDhDNSIsInZpc2l0b3JfaWQiOiI2Njc5NDM2OTYzMzMxODIzODEzIiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0=
visitor-hmacfc9db3e8301cfd3fa2c3e601772e3464e9506ada706813d3e79a24f1358c19e6
hovercard-subject-tagissue:3286970301
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/62182/issue_layout
twitter:imagehttps://opengraph.githubassets.com/640381ef8418be43187dc6fc1d047a02ac79bd38210406d84b5aa3e96ad3ad63/microsoft/TypeScript/issues/62182
twitter:cardsummary_large_image
og:imagehttps://opengraph.githubassets.com/640381ef8418be43187dc6fc1d047a02ac79bd38210406d84b5aa3e96ad3ad63/microsoft/TypeScript/issues/62182
og:image:alt🔍 Search Terms generic infer generic typeof typeof arguments infer-only generics infer arguments unoverridable generics ✅ Viability Checklist This wouldn't be a breaking change in existing TypeScri...
og:image:width1200
og:image:height600
og:site_nameGitHub
og:typeobject
og:author:usernamerintaun
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/62182#start-of-content
https://github.com/
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fmicrosoft%2FTypeScript%2Fissues%2F62182
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%2F62182
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/62182
Reloadhttps://github.com/microsoft/TypeScript/issues/62182
Reloadhttps://github.com/microsoft/TypeScript/issues/62182
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/62182
New issuehttps://github.com/login?return_to=https://github.com/microsoft/TypeScript/issues/62182
Infer-only function genericshttps://github.com/microsoft/TypeScript/issues/62182#top
DuplicateAn existing issue was already createdhttps://github.com/microsoft/TypeScript/issues?q=state%3Aopen%20label%3A%22Duplicate%22
https://github.com/rintaun
Matthew Lanigan (rintaun)https://github.com/rintaun
on Aug 3, 2025https://github.com/microsoft/TypeScript/issues/62182#issue-3286970301
https://github.com/microsoft/TypeScript/wiki/No-New-Utility-Typeshttps://github.com/microsoft/TypeScript/wiki/No-New-Utility-Types
https://github.com/Microsoft/TypeScript/wiki/TypeScript-Design-Goalshttps://github.com/Microsoft/TypeScript/wiki/TypeScript-Design-Goals
#23696https://github.com/microsoft/TypeScript/pull/23696
#26242https://github.com/microsoft/TypeScript/issues/26242
https://github.com/MartinJohns
Martin Johns (MartinJohns)https://github.com/MartinJohns
on Aug 3, 2025https://github.com/microsoft/TypeScript/issues/62182#issuecomment-3148331370
#59814https://github.com/microsoft/TypeScript/issues/59814
https://github.com/rintaun
Matthew Lanigan (rintaun)https://github.com/rintaun
on Aug 3, 2025https://github.com/microsoft/TypeScript/issues/62182#issuecomment-3148432502
#59814 (comment)https://github.com/microsoft/TypeScript/issues/59814#issuecomment-2628485034
https://github.com/MartinJohns
Martin Johns (MartinJohns)https://github.com/MartinJohns
on Aug 3, 2025https://github.com/microsoft/TypeScript/issues/62182#issuecomment-3148457436
#26242https://github.com/microsoft/TypeScript/issues/26242
#10571https://github.com/microsoft/TypeScript/issues/10571
https://github.com/rintaun
Matthew Lanigan (rintaun)https://github.com/rintaun
on Aug 3, 2025https://github.com/microsoft/TypeScript/issues/62182#issuecomment-3148502107
https://github.com/MartinJohns
Martin Johns (MartinJohns)https://github.com/MartinJohns
on Aug 3, 2025https://github.com/microsoft/TypeScript/issues/62182#issuecomment-3148743121
https://github.com/rintaun
Matthew Lanigan (rintaun)https://github.com/rintaun
on Aug 3, 2025https://github.com/microsoft/TypeScript/issues/62182#issuecomment-3148765234
https://github.com/MartinJohns
Martin Johns (MartinJohns)https://github.com/MartinJohns
on Aug 4, 2025https://github.com/microsoft/TypeScript/issues/62182#issuecomment-3148914897
https://github.com/rintaun
Matthew Lanigan (rintaun)https://github.com/rintaun
on Aug 4, 2025https://github.com/microsoft/TypeScript/issues/62182#issuecomment-3149023643
https://github.com/MartinJohns
Martin Johns (MartinJohns)https://github.com/MartinJohns
on Aug 4, 2025https://github.com/microsoft/TypeScript/issues/62182#issuecomment-3149126607
https://github.com/rintaun
Matthew Lanigan (rintaun)https://github.com/rintaun
on Aug 4, 2025https://github.com/microsoft/TypeScript/issues/62182#issuecomment-3150171453
https://github.com/MartinJohns
Martin Johns (MartinJohns)https://github.com/MartinJohns
on Aug 4, 2025https://github.com/microsoft/TypeScript/issues/62182#issuecomment-3150228009
#60222 (comment)https://github.com/microsoft/TypeScript/issues/60222#issuecomment-2412073279
#26242https://github.com/microsoft/TypeScript/issues/26242
https://github.com/RyanCavanaugh
Ryan Cavanaugh (RyanCavanaugh)https://github.com/RyanCavanaugh
on Aug 18, 2025https://github.com/microsoft/TypeScript/issues/62182#issuecomment-3198735190
Proposal: Partial Type Argument Inference #26242https://github.com/microsoft/TypeScript/issues/26242
Allow skipping some generics when calling a function with multiple generics #10571https://github.com/microsoft/TypeScript/issues/10571
Type subscope for local type aliases #41470https://github.com/microsoft/TypeScript/issues/41470
Allow defering type check of default value for generic-typed function parameter until instantiation #58977https://github.com/microsoft/TypeScript/issues/58977
Introduce "must-infer" generics to restrict user-specified type overrides #59814https://github.com/microsoft/TypeScript/issues/59814
#59814https://github.com/microsoft/TypeScript/issues/59814
Ryan Cavanaugh (RyanCavanaugh)https://github.com/RyanCavanaugh
DuplicateAn existing issue was already createdhttps://github.com/microsoft/TypeScript/issues?q=state%3Aopen%20label%3A%22Duplicate%22
on Aug 18, 2025https://github.com/microsoft/TypeScript/issues/62182#event-19213667682
https://github.com/typescript-bot
TypeScript Bot (typescript-bot)https://github.com/typescript-bot
on Aug 21, 2025https://github.com/microsoft/TypeScript/issues/62182#issuecomment-3208634659
TypeScript Bot (typescript-bot)https://github.com/typescript-bot
not plannedhttps://github.com/microsoft/TypeScript/issues?q=is%3Aissue%20state%3Aclosed%20archived%3Afalse%20reason%3Anot-planned
on Aug 21, 2025https://github.com/microsoft/TypeScript/issues/62182#event-19256575724
microsofthttps://github.com/microsoft
on Oct 22, 2025https://github.com/microsoft/TypeScript/issues/62182#event-20419722012
Sign up for freehttps://github.com/signup?return_to=https://github.com/microsoft/TypeScript/issues/62182
Sign in to commenthttps://github.com/login?return_to=https://github.com/microsoft/TypeScript/issues/62182
DuplicateAn existing issue was already createdhttps://github.com/microsoft/TypeScript/issues?q=state%3Aopen%20label%3A%22Duplicate%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.