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
Domain: github.com
IPv4 address: 140.82.114.4
{"@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\r\n\r\n\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-profiling | 0 |
| react-import-map | react,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-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:70100d12-56a4-5ffd-f1c1-ee8f5cb2966b |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 8D84:1453CC:15D576:1E472C:6B046559 |
| html-safe-nonce | f587541c5097c5c9d5fdc0778a852930e359123f8bab7416a853b83e397cfd40 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4RDg0OjE0NTNDQzoxNUQ1NzY6MUU0NzJDOjZCMDQ2NTU5IiwidmlzaXRvcl9pZCI6IjQyODgxODYxNTg2MTUzODk1MjkiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | b1471ec5dc8458ce88a9f97ea1d251f648c2e11340cc323c5f00fa1beffa83e7 |
| hovercard-subject-tag | issue:542868783 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/microsoft/TypeScript/35873/issue_layout |
| twitter:image | https://opengraph.githubassets.com/ea7ddfbd259d2a3569539befe367b269f300b0b7f813829ff60afbd3ea89b45e/microsoft/TypeScript/issues/35873 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/ea7ddfbd259d2a3569539befe367b269f300b0b7f813829ff60afbd3ea89b45e/microsoft/TypeScript/issues/35873 |
| og:image:alt | Search Terms "co dependent arguments" "co-dependent arguments" "codependent arguments" "codependently arguments" "codependently typed arguments" Suggestion It should be easier to conditionally type... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | nathggns |
| hostname | github.com |
| expected-hostname | github.com |
| None | 83e4c0e58c5162177f3c161c5fbad38527ae04c11ac14980f9f9d85ae2027b1f |
| turbo-cache-control | no-preview |
| go-import | github.com/microsoft/TypeScript git https://github.com/microsoft/TypeScript.git |
| octolytics-dimension-user_id | 6154722 |
| octolytics-dimension-user_login | microsoft |
| octolytics-dimension-repository_id | 20929025 |
| octolytics-dimension-repository_nwo | microsoft/TypeScript |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 20929025 |
| octolytics-dimension-repository_network_root_nwo | microsoft/TypeScript |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | ca4d07a7cef4e4b75f821dc25e7300d80c1450a9 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
| Server | github.com |
| Content-Type | text/html; charset=utf-8 |
| Cache-Control | max-age=0, private, must-revalidate |
Viewport: width=device-width