René's URL Explorer Experiment


Title: Named/keyed type parameters · Issue #54254 · microsoft/TypeScript · GitHub

Open Graph Title: Named/keyed type parameters · Issue #54254 · microsoft/TypeScript

X Title: Named/keyed type parameters · Issue #54254 · microsoft/TypeScript

Description: Suggestion 🔍 Search Terms named type parameters generic bag ✅ Viability Checklist My suggestion meets these guidelines: This wouldn't be a breaking change in existing TypeScript/JavaScript code This wouldn't change the runtime behavior o...

Open Graph Description: Suggestion 🔍 Search Terms named type parameters generic bag ✅ Viability Checklist My suggestion meets these guidelines: This wouldn't be a breaking change in existing TypeScript/JavaScript code Thi...

X Description: Suggestion 🔍 Search Terms named type parameters generic bag ✅ Viability Checklist My suggestion meets these guidelines: This wouldn't be a breaking change in existing TypeScript/JavaScript code...

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

X: @github

direct link

Domain: github.com


Hey, it has json ld scripts:
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Named/keyed type parameters","articleBody":"# Suggestion\r\n\r\n## 🔍 Search Terms\r\n\r\nnamed type parameters generic bag\r\n\r\n## ✅ Viability Checklist\r\n\r\nMy suggestion meets these guidelines:\r\n\r\n* [x] This wouldn't be a breaking change in existing TypeScript/JavaScript code\r\n* [x] This wouldn't change the runtime behavior of existing JavaScript code\r\n* [x] This could be implemented without emitting different JS based on the types of the expressions\r\n* [x] This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)\r\n* [x] This feature would agree with the rest of [TypeScript's Design Goals](https://github.com/Microsoft/TypeScript/wiki/TypeScript-Design-Goals).\r\n\r\n\r\n## ⭐ Suggestion\r\n\r\n_Disclaimer_: I somewhat suspect that a similar feature request exists already but I couldn't find any.\r\n\r\nI'd like to suggest an ability to specify **named type parameters**. Some libraries have long lists of generic type parameters (XState currently sits at 5: [here](https://github.com/statelyai/xstate/blob/de75010fd943900eb1c776a87c2283531e46cbf0/packages/core/src/Machine.ts#L85-L89)) and it becomes hard for consumers to remember the order of those.\r\n\r\nI think that it might be hard for me to beat @weswigham's arguments from [here](https://github.com/microsoft/TypeScript/pull/20126):\r\n\r\n> This could be particularly useful in Vue, as then they could introduce a bag for the type parameters passed into their many generic functions. This would enable them to add more type arguments as needed for improved inference or for future features, without breaking existing consumers who are manually specifying a subset of parameters (simply make new arguments in the bag optional) and also allowing that bag to be extended (via interface reopening) by any vue plugins, such as vuex (which then enables them to add overloads to vue-core methods which can carry through the new type information they provide).\r\n\r\nI think that it's best to model this feature after objects and destructuring patterns. That should create a familiar syntax for end users. \r\n\r\nI'm not really married to any particular syntax and I think it's somewhere up for debate, one variation that comes to mind is this:\r\n```ts\r\ndeclare function fn\u003c{ T extends number; T2 extends string; }>(a: T, b: T2): void\r\n\r\nfn\u003c{ T: 100, T2: 'foo' }>(100, 'foo')\r\n```\r\n\r\n## 📃 Motivating Example\r\n\r\nAny library with more than 2-3 type parameters. Vue, TanStack Query, XState and more come to mind.\r\n\r\nCurrently TanStack query has such an overload for its `useQuery`:\r\n```ts\r\nexport function useQuery\u003c\r\n  TQueryFnData = unknown,\r\n  TError = unknown,\r\n  TData = TQueryFnData,\r\n  TQueryKey extends QueryKey = QueryKey,\r\n>(\r\n  options: Omit\u003c\r\n    UseQueryOptions\u003cTQueryFnData, TError, TData, TQueryKey>,\r\n    'initialData'\r\n  > & { initialData: TQueryFnData | (() => TQueryFnData) },\r\n): DefinedUseQueryResult\u003cTData, TError>\r\n```\r\n\r\nThis could likely be rewritten to something like:\r\n```ts\r\nexport function useQuery\u003c{\r\n  fnData = unknown,\r\n  error = unknown,\r\n  data = fnData,\r\n  key extends QueryKey = QueryKey,\r\n}>(\r\n  options: Omit\u003c\r\n    UseQueryOptions\u003cfnData, error, data, key>,\r\n    'initialData'\r\n  > & { initialData: fnData | (() => fnData) },\r\n): DefinedUseQueryResult\u003cdata, error>\r\n```\r\n\r\nA nice trait of this feature would be that generic names would become **autocompletable**. Today we might rely on signature help when typing but it doesn't provide the ideal experience. It has too much information (even if the interesting piece of information is in bold), it's squished into a single line and it's hard to focus on what we are typing (plus, of course, we can't selectively start typing those type parameters \"out of order\").\r\n\r\n## 💻 Use Cases\r\n\r\nCurrently one might use a \"generic bag\" to imitate this feature. Using the previous example this could look like:\r\n```ts\r\nexport function useQuery\u003cT extends { fnData: unknown; error: unknown; data: unknown; key: QueryKey }>(\r\n  options: Omit\u003c\r\n    UseQueryOptions\u003cT['fnData'], T['error'], T['data'], T['key']>,\r\n    'initialData'\r\n  > & { initialData: T['fnData'] | (() => T['fnData']) },\r\n): DefinedUseQueryResult\u003cT['data'], T['error']>\r\n```\r\n\r\nOne of the problems with this is that it's not easy to add defaults to specific slots, one has to resort to helper types like `WithDefaults` and implement that logic on their own.\r\n\r\nIn fact, I currently have an [open PR](https://github.com/microsoft/TypeScript/pull/53017) that would allow to infer `T` using such indexes like in the example above (this PR builds on top of an already referenced [@weswigham's PR](https://github.com/microsoft/TypeScript/pull/20126)).\r\n\r\nMy primary motivation is to enable such inference (based on indexes) for **reverse mapped types**. It's important for those to create *multiple* relationships within a single object property value/tuple element. It could be a nice addition for other type parameters that would make this feature more consistent. However, I think that when it comes to regular type parameters this approach has an important disadvantage when compared to this proposal.\r\n\r\nInferring using indexes doesn't provide the same capabilities as inferring to naked type parameters:\r\n1. we can't easily default them\r\n2. we can't rely on different sets of type parameter modifiers (`in`/`out`/`const`) for each \"slot\" (it's not possible to annotate part of the type parameter)\r\n3. it's hard to reason about variance of those \"slots\", I'm not even quite sure how such indexes behave when it comes to variance\r\n4. the upcoming partial inference sigil won't be applicable for such a \"slot\"\r\n\r\nI think that this featue is important to address those concerns and to enhance the flexibility for library authors.\r\n\r\nWhen it comes to the implementation... I think that this could be added fairly easily. The main things that would have to be added to add support for this would be the changes in the parser and code \"matching\" the names with the parameters list. The parameters lists should still be kept internally as a flat array and matching would be implemented only on boundaries.","author":{"url":"https://github.com/Andarist","@type":"Person","name":"Andarist"},"datePublished":"2023-05-15T17:33:24Z","url":"https://github.com/microsoft/TypeScript/issues/54254"}

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:6d0bb805-e8b1-4ff7-c7bf-4ff8a951976d
current-catalog-service-hash81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114
request-idBCA4:14EB6E:D9B8C9:11E9D69:6B01CB51
html-safe-nonce03a9785f36708b0c198c1b0edaadb31716ac0d4a356a11550af89ff4ae609e66
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCQ0E0OjE0RUI2RTpEOUI4Qzk6MTFFOUQ2OTo2QjAxQ0I1MSIsInZpc2l0b3JfaWQiOiI1MzMyMTQwMTU1MzI5NDMyNDAxIiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0=
visitor-hmace47d9add0fca89c74ee54933593371a2669d37d2e52a16f01cd03e6e9886b52c
hovercard-subject-tagissue:1710521592
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/54254/issue_layout
twitter:imagehttps://opengraph.githubassets.com/a55b858b182c6613eff77c46d8db007d5b986f5e454bc28fa77529d79c113ad7/microsoft/TypeScript/issues/54254
twitter:cardsummary_large_image
og:imagehttps://opengraph.githubassets.com/a55b858b182c6613eff77c46d8db007d5b986f5e454bc28fa77529d79c113ad7/microsoft/TypeScript/issues/54254
og:image:altSuggestion 🔍 Search Terms named type parameters generic bag ✅ Viability Checklist My suggestion meets these guidelines: This wouldn't be a breaking change in existing TypeScript/JavaScript code Thi...
og:image:width1200
og:image:height600
og:site_nameGitHub
og:typeobject
og:author:usernameAndarist
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/54254#start-of-content
https://github.com/
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fmicrosoft%2FTypeScript%2Fissues%2F54254
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%2F54254
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/54254
Reloadhttps://github.com/microsoft/TypeScript/issues/54254
Reloadhttps://github.com/microsoft/TypeScript/issues/54254
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/54254
New issuehttps://github.com/login?return_to=https://github.com/microsoft/TypeScript/issues/54254
Named/keyed type parametershttps://github.com/microsoft/TypeScript/issues/54254#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/Andarist
Mateusz Burzyński (Andarist)https://github.com/Andarist
on May 15, 2023https://github.com/microsoft/TypeScript/issues/54254#issue-1710521592
TypeScript's Design Goalshttps://github.com/Microsoft/TypeScript/wiki/TypeScript-Design-Goals
herehttps://github.com/statelyai/xstate/blob/de75010fd943900eb1c776a87c2283531e46cbf0/packages/core/src/Machine.ts#L85-L89
Wesley Wigham (@weswigham)https://github.com/weswigham
herehttps://github.com/microsoft/TypeScript/pull/20126
open PRhttps://github.com/microsoft/TypeScript/pull/53017
@weswigham's PRhttps://github.com/microsoft/TypeScript/pull/20126
https://github.com/fatcerberus
Bruce Pascoe (fatcerberus)https://github.com/fatcerberus
on May 15, 2023https://github.com/microsoft/TypeScript/issues/54254#issuecomment-1548334023
#38913https://github.com/microsoft/TypeScript/issues/38913
https://github.com/ssalbdivad
David Blass (ssalbdivad)https://github.com/ssalbdivad
on May 15, 2023https://github.com/microsoft/TypeScript/issues/54254#issuecomment-1548335946
Mateusz Burzyński (@Andarist)https://github.com/Andarist
herehttps://github.com/microsoft/TypeScript/pull/52241
herehttps://github.com/microsoft/TypeScript/pull/26349
https://github.com/fatcerberus
Bruce Pascoe (fatcerberus)https://github.com/fatcerberus
on May 15, 2023https://github.com/microsoft/TypeScript/issues/54254#issuecomment-1548342814
https://github.com/ssalbdivad
David Blass (ssalbdivad)https://github.com/ssalbdivad
on May 15, 2023https://github.com/microsoft/TypeScript/issues/54254#issuecomment-1548350163
Bruce Pascoe (@fatcerberus)https://github.com/fatcerberus
https://github.com/Andarist
Mateusz Burzyński (Andarist)https://github.com/Andarist
on May 15, 2023https://github.com/microsoft/TypeScript/issues/54254#issuecomment-1548355825
Ron Buckton (@rbuckton)https://github.com/rbuckton
https://github.com/tc39/proposal-extractorshttps://github.com/tc39/proposal-extractors
https://github.com/phryneas
Lenz Weber-Tronic (phryneas)https://github.com/phryneas
on May 15, 2023https://github.com/microsoft/TypeScript/issues/54254#issuecomment-1548495593
https://github.com/reduxjs/redux-toolkit/blob/e9dfc9384fb2f92eba75335b8b6f293c3c469269/packages/toolkit/src/createAsyncThunk.ts#L402-L406https://github.com/reduxjs/redux-toolkit/blob/e9dfc9384fb2f92eba75335b8b6f293c3c469269/packages/toolkit/src/createAsyncThunk.ts#L402-L406
https://github.com/jcalz
Joe Calzaretta (jcalz)https://github.com/jcalz
on May 15, 2023https://github.com/microsoft/TypeScript/issues/54254#issuecomment-1548525911
#20126https://github.com/microsoft/TypeScript/pull/20126
https://github.com/fatcerberus
Bruce Pascoe (fatcerberus)https://github.com/fatcerberus
on May 15, 2023https://github.com/microsoft/TypeScript/issues/54254#issuecomment-1548536321
Mateusz Burzyński (@Andarist)https://github.com/Andarist
Joe Calzaretta (@jcalz)https://github.com/jcalz
https://github.com/Andarist
Mateusz Burzyński (Andarist)https://github.com/Andarist
on May 15, 2023https://github.com/microsoft/TypeScript/issues/54254#issuecomment-1548704044
Joe Calzaretta (@jcalz)https://github.com/jcalz
#20126https://github.com/microsoft/TypeScript/pull/20126
https://github.com/jcalz
Joe Calzaretta (jcalz)https://github.com/jcalz
on May 15, 2023https://github.com/microsoft/TypeScript/issues/54254#issuecomment-1548710577
https://github.com/bowheart
Josh C (bowheart)https://github.com/bowheart
on May 16, 2023https://github.com/microsoft/TypeScript/issues/54254#issuecomment-1548882293
Mateusz Burzyński (@Andarist)https://github.com/Andarist
#54047https://github.com/microsoft/TypeScript/pull/54047
Here's one real-world examplehttps://github.com/Omnistac/zedux/blob/master/packages/atoms/src/classes/instances/AtomInstance.ts#L71-L74
#3https://github.com/microsoft/TypeScript/pull/3
on this Zedux doc pagehttps://omnistac.github.io/zedux/docs/advanced/typescript-tips#anyatomtemplate
https://github.com/eloytoro
Eloy Toro (eloytoro)https://github.com/eloytoro
on May 16, 2023https://github.com/microsoft/TypeScript/issues/54254#issuecomment-1549247051
https://github.com/Andarist
Mateusz Burzyński (Andarist)https://github.com/Andarist
on May 16, 2023https://github.com/microsoft/TypeScript/issues/54254#issuecomment-1549264819
https://github.com/eloytoro
Eloy Toro (eloytoro)https://github.com/eloytoro
on May 16, 2023https://github.com/microsoft/TypeScript/issues/54254#issuecomment-1549269347
https://github.com/Andarist
Mateusz Burzyński (Andarist)https://github.com/Andarist
on May 16, 2023https://github.com/microsoft/TypeScript/issues/54254#issuecomment-1549269494
Josh C (@bowheart)https://github.com/bowheart
https://github.com/bowheart
Josh C (bowheart)https://github.com/bowheart
on May 16, 2023https://github.com/microsoft/TypeScript/issues/54254#issuecomment-1549566832
#54047https://github.com/microsoft/TypeScript/pull/54047
Ryan Cavanaugh (RyanCavanaugh)https://github.com/RyanCavanaugh
SuggestionAn idea for TypeScripthttps://github.com/microsoft/TypeScript/issues?q=state%3Aopen%20label%3A%22Suggestion%22
In DiscussionNot yet reached consensushttps://github.com/microsoft/TypeScript/issues?q=state%3Aopen%20label%3A%22In%20Discussion%22
on May 16, 2023https://github.com/microsoft/TypeScript/issues/54254#event-9261645215
https://github.com/jedwards1211
Andy Edwards (jedwards1211)https://github.com/jedwards1211
on May 28, 2023https://github.com/microsoft/TypeScript/issues/54254#issuecomment-1566209179
#23696 (comment)https://github.com/microsoft/TypeScript/pull/23696#issuecomment-408717985
Josh C (@bowheart)https://github.com/bowheart
https://github.com/jedwards1211
Andy Edwards (jedwards1211)https://github.com/jedwards1211
on May 28, 2023https://github.com/microsoft/TypeScript/issues/54254#issuecomment-1566213948
Mateusz Burzyński (@Andarist)https://github.com/Andarist
https://github.com/c7hm4r
Christoph Müller (c7hm4r)https://github.com/c7hm4r
on Aug 4, 2023https://github.com/microsoft/TypeScript/issues/54254#issuecomment-1665694831
Eloy Toro (@eloytoro)https://github.com/eloytoro
fastifyhttps://github.com/fastify/fastify/blob/c63fa60b7695f284ab8bcced9ac4f58f5eb173d3/types/reply.d.ts#L31C1-L40C2
https://github.com/so1ve
Ray (so1ve)https://github.com/so1ve
on Sep 1, 2023https://github.com/microsoft/TypeScript/issues/54254#issuecomment-1702940510
https://github.com/kronodeus
Ryan Palmer (kronodeus)https://github.com/kronodeus
on Oct 31, 2023https://github.com/microsoft/TypeScript/issues/54254#issuecomment-1787658283
Sign up for freehttps://github.com/signup?return_to=https://github.com/microsoft/TypeScript/issues/54254
Sign in to commenthttps://github.com/login?return_to=https://github.com/microsoft/TypeScript/issues/54254
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.