René's URL Explorer Experiment


Title: Support for co-dependently conditionally typed arguments · Issue #35873 · microsoft/TypeScript · GitHub

Open Graph Title: Support for co-dependently conditionally typed arguments · Issue #35873 · microsoft/TypeScript

X Title: Support for co-dependently conditionally typed arguments · Issue #35873 · microsoft/TypeScript

Description: Search Terms "co dependent arguments" "co-dependent arguments" "codependent arguments" "codependently arguments" "codependently typed arguments" Suggestion It should be easier to conditionally type a function argument based on the type/v...

Open Graph Description: Search Terms "co dependent arguments" "co-dependent arguments" "codependent arguments" "codependently arguments" "codependently typed arguments" Suggestion It should be easier to conditionally type...

X Description: Search Terms "co dependent arguments" "co-dependent arguments" "codependent arguments" "codependently arguments" "codependently typed arguments" Su...

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

X: @github

direct link

Domain: github.com

IPv4 address: 140.82.114.4


Hey, it has json ld scripts:
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Support for co-dependently conditionally typed arguments","articleBody":"\u003c!-- 🚨 STOP 🚨 𝗦𝗧𝗢𝗣 🚨 𝑺𝑻𝑶𝑷 🚨\r\n\r\nHalf of all issues filed here are duplicates, answered in the FAQ, or not appropriate for the bug tracker.\r\n\r\nPlease help us by doing the following steps before logging an issue:\r\n  * Search: https://github.com/Microsoft/TypeScript/search?type=Issues\r\n  * Read the FAQ, especially the \"Common Feature Requests\" section: https://github.com/Microsoft/TypeScript/wiki/FAQ\r\n\r\n-->\r\n\r\n## Search Terms\r\n\r\n- \"co dependent arguments\"\r\n- \"co-dependent arguments\"\r\n- \"codependent arguments\"\r\n- \"codependently arguments\"\r\n- \"codependently typed arguments\"\r\n\r\n\u003c!-- List of keywords you searched for before creating this issue. Write them down here so that others can find this suggestion more easily -->\r\n\r\n## Suggestion\r\n\r\nIt should be easier to conditionally type a function argument based on the type/value (in the case of enums / string literal types) of a previous argument in the function. Having a more specific way to do this than currently possible should improve editor intelligence and performance. \r\n\r\n\u003c!-- A summary of what you'd like to see added or changed -->\r\n\r\n## Use Cases\r\n\r\nIt is quite common to have an API where the type of the second argument depends on the type and/or value of the first. For example, take a look at the following JavaScript code. (It's a fairly trivial example, but it should illustrate the use case.\r\n\r\n```js\r\nfunction reducer(actionType, args) {\r\n    switch (actionType) {\r\n      case \"add\":\r\n        console.log(args.a + args.b);\r\n        break;\r\n\r\n      case \"concat\":\r\n        console.log(args.firstArr.concat(args.secondArr));\r\n        break;\r\n    }\r\n}\r\n\r\nreducer(\"add\", { a: 1, b: 3 }); // 4\r\nreducer(\"concat\", { firstArr: [1,2], secondArr: [3, 4] }); // [1,2,3,4]\r\n```\r\n\r\nIt is currently possible to type this, but it is extremely convulted and the editor somewhat struggles to understand this at the call site. This method is based on the fantastic article over at https://artsy.github.io/blog/2018/11/21/conditional-types-in-typescript/\r\n\r\n```ts\r\nenum ActionTypeEnum {\r\n  add,\r\n  concat\r\n}\r\n\r\ntype Action =\r\n  | { type: ActionTypeEnum.add, a: number, b: number }\r\n  | { type: ActionTypeEnum.concat, firstArr: any[], secondArr: any[] };\r\n\r\ntype ActionType = Action[\"type\"];\r\n\r\ntype ExcludeTypeKey\u003cK> = K extends \"type\" ? never : K\r\n\r\ntype ExcludeTypeField\u003cA> = { [K in ExcludeTypeKey\u003ckeyof A>]: A[K] }\r\n\r\ntype ExtractActionParameters\u003cA, T> = A extends { type: T }\r\n    ? ExcludeTypeField\u003cA>\r\n  : never\r\n\r\nfunction reducer\u003cT extends ActionType>(actionType: T, args: ExtractActionParameters\u003cAction, T>) {\r\n  const properlyTypedArgs = { ...args, type: actionType } as Action;\r\n\r\n    switch (properlyTypedArgs.type) {\r\n      case ActionTypeEnum.add:\r\n        console.log(properlyTypedArgs.a + properlyTypedArgs.b);\r\n        break;\r\n\r\n      case ActionTypeEnum.concat:\r\n        console.log(properlyTypedArgs.firstArr.concat(properlyTypedArgs.secondArr));\r\n        break;\r\n    }\r\n}\r\n\r\nreducer(ActionTypeEnum.add, { a: 1, b: 3 }); // 4\r\nreducer(ActionTypeEnum.concat, { firstArr: [1, 2], secondArr: [3, 4] }); // [1,2,3,4]\r\n```\r\n\r\nThere are four main issues with this approach.\r\n\r\nThe first, fairly obvious one, is that for the type checker to understand that type of args inside the switch, we're having to merge the two arguments into a single object, cast it, and then switch based on that:\r\n\r\n```ts\r\nconst properlyTypedArgs = { ...args, type: actionType } as Action;\r\n```\r\n\r\nThis is an example of the type system leaking into the emitted javascript and is arguably inefficient. \r\n\r\nThe second is that the editor doesn't really understand what's going on. It understands enough to tell you you've made an error when the type check doesn't validate but also doesn't understand well enough to properly autocomplete. The error could also be easier to understand. See below. \r\n\r\n![image](https://user-images.githubusercontent.com/719814/71518037-abca4c80-28a8-11ea-8c45-32b535612836.png)\r\n\r\n![image](https://user-images.githubusercontent.com/719814/71518048-b4bb1e00-28a8-11ea-8fa8-3e39f8a4821d.png)\r\n\r\nSee above how it's suggesting firstArr and secondArr as fields in the second object, even though including them fails the type checker. The only suggestions should be a & b. \r\n\r\nThe third issue is that it's entirely undiscoverable for all but the most advanced in TypeScript. This is a semi-common use case, and I had no idea how to properly type it. Without this singular article detailing this approach, I never would have discovered it. This should be built into the language to fix this.\r\n\r\nFourthly, and similarly, it's very difficult to read and understand unless you really, really understand generics. I am sure that somebody smarter than I would be able to come up with a more expressive way of detailing the co-dependence of these two types. \r\n\r\n\r\n\u003c!--\r\nWhat do you want to use this for?\r\nWhat shortcomings exist with current approaches?\r\n-->\r\n\r\n## Examples\r\n\r\nI think I've properly explained this in the use case above. \r\n\r\n\u003c!-- Show how this would be used and what the behavior would be -->\r\n\r\n## 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, 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","author":{"url":"https://github.com/nathggns","@type":"Person","name":"nathggns"},"datePublished":"2019-12-27T13:02:31Z","url":"https://github.com/microsoft/TypeScript/issues/35873"}

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:70100d12-56a4-5ffd-f1c1-ee8f5cb2966b
current-catalog-service-hash81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114
request-id8D84:1453CC:15D576:1E472C:6B046559
html-safe-noncef587541c5097c5c9d5fdc0778a852930e359123f8bab7416a853b83e397cfd40
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4RDg0OjE0NTNDQzoxNUQ1NzY6MUU0NzJDOjZCMDQ2NTU5IiwidmlzaXRvcl9pZCI6IjQyODgxODYxNTg2MTUzODk1MjkiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ==
visitor-hmacb1471ec5dc8458ce88a9f97ea1d251f648c2e11340cc323c5f00fa1beffa83e7
hovercard-subject-tagissue:542868783
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/35873/issue_layout
twitter:imagehttps://opengraph.githubassets.com/ea7ddfbd259d2a3569539befe367b269f300b0b7f813829ff60afbd3ea89b45e/microsoft/TypeScript/issues/35873
twitter:cardsummary_large_image
og:imagehttps://opengraph.githubassets.com/ea7ddfbd259d2a3569539befe367b269f300b0b7f813829ff60afbd3ea89b45e/microsoft/TypeScript/issues/35873
og:image:altSearch Terms "co dependent arguments" "co-dependent arguments" "codependent arguments" "codependently arguments" "codependently typed arguments" Suggestion It should be easier to conditionally type...
og:image:width1200
og:image:height600
og:site_nameGitHub
og:typeobject
og:author:usernamenathggns
hostnamegithub.com
expected-hostnamegithub.com
None83e4c0e58c5162177f3c161c5fbad38527ae04c11ac14980f9f9d85ae2027b1f
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
releaseca4d07a7cef4e4b75f821dc25e7300d80c1450a9
ui-targetfull
theme-color#1e2327
color-schemelight dark
react-app-nameissues

Links:

Skip to contenthttps://github.com/microsoft/TypeScript/issues/35873#start-of-content
https://github.com/
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fmicrosoft%2FTypeScript%2Fissues%2F35873
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%2F35873
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/35873
Reloadhttps://github.com/microsoft/TypeScript/issues/35873
Reloadhttps://github.com/microsoft/TypeScript/issues/35873
microsofthttps://github.com/microsoft
TypeScripthttps://github.com/microsoft/TypeScript
Codehttps://github.com/microsoft/TypeScript
Issues5k (5k)https://github.com/microsoft/TypeScript/issues
Pull requests319 (319)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/35873
New issuehttps://github.com/login?return_to=https://github.com/microsoft/TypeScript/issues/35873
Support for co-dependently conditionally typed argumentshttps://github.com/microsoft/TypeScript/issues/35873#top
Needs ProposalThis issue needs a plan that clarifies the finer details of how it could be implemented.https://github.com/microsoft/TypeScript/issues?q=state%3Aopen%20label%3A%22Needs%20Proposal%22
SuggestionAn idea for TypeScripthttps://github.com/microsoft/TypeScript/issues?q=state%3Aopen%20label%3A%22Suggestion%22
https://github.com/nathggns
Nate Higgins (nathggns)https://github.com/nathggns
on Dec 27, 2019https://github.com/microsoft/TypeScript/issues/35873#issue-542868783
https://artsy.github.io/blog/2018/11/21/conditional-types-in-typescript/https://artsy.github.io/blog/2018/11/21/conditional-types-in-typescript/
https://user-images.githubusercontent.com/719814/71518037-abca4c80-28a8-11ea-8c45-32b535612836.png
https://user-images.githubusercontent.com/719814/71518048-b4bb1e00-28a8-11ea-8fa8-3e39f8a4821d.png
TypeScript's Design Goalshttps://github.com/Microsoft/TypeScript/wiki/TypeScript-Design-Goals
https://github.com/jcalz
Joe Calzaretta (jcalz)https://github.com/jcalz
on Dec 27, 2019https://github.com/microsoft/TypeScript/issues/35873#issuecomment-569307018
#30581https://github.com/microsoft/TypeScript/issues/30581
Playground Linkhttps://www.typescriptlang.org/play/#code/C4TwDgpgBAShAmBXAxhATgQTQcwM5QF4oBtAIgEN55SAaKAbynIC4oA7RAWwCN07vWHHuigBfALpQAPgFgAUFBKlkAezbJywWgygAzAJZpcwLGlbk2IYuLq4IqtvFPnL1seIDc8+bsTrg+mpQaAgo6AAUAHTR5Di4rHBIqJhxAJQM8oq4AO76wMgAFlDhsXjEAAzi6fSZilAadlAUVKTMtXX1argqADYQkT0q2CVxxACM4pHkUADUTKMTkdypXgodUNwh5ADWq+315I3KahpabWsdDt19A0MjZYsGRiZoaJEOp-e445N2Dk6vVIrfaKTYQHarRSieTQuQhJIRZrUOiMFhQMb8VgAZjEKygAHp8VAACzyeFhNDhY7qTTaRhPYzOEgYqAAJhsUD+agBZhIWLoxMkojxhOZNFZNH5gvkQA
Ryan Cavanaugh (RyanCavanaugh)https://github.com/RyanCavanaugh
Needs ProposalThis issue needs a plan that clarifies the finer details of how it could be implemented.https://github.com/microsoft/TypeScript/issues?q=state%3Aopen%20label%3A%22Needs%20Proposal%22
SuggestionAn idea for TypeScripthttps://github.com/microsoft/TypeScript/issues?q=state%3Aopen%20label%3A%22Suggestion%22
on Jan 6, 2020https://github.com/microsoft/TypeScript/issues/35873#event-2925860028
https://github.com/RyanCavanaugh
Ryan Cavanaugh (RyanCavanaugh)https://github.com/RyanCavanaugh
on Jan 6, 2020https://github.com/microsoft/TypeScript/issues/35873#issuecomment-571362487
#33014https://github.com/microsoft/TypeScript/issues/33014
Sign up for freehttps://github.com/signup?return_to=https://github.com/microsoft/TypeScript/issues/35873
Sign in to commenthttps://github.com/login?return_to=https://github.com/microsoft/TypeScript/issues/35873
Needs ProposalThis issue needs a plan that clarifies the finer details of how it could be implemented.https://github.com/microsoft/TypeScript/issues?q=state%3Aopen%20label%3A%22Needs%20Proposal%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

Response headers:
Servergithub.com
Content-Typetext/html; charset=utf-8
Cache-Controlmax-age=0, private, must-revalidate

Viewport: width=device-width


URLs of crawlers that visited me.