Title: Allow skipping some generics when calling a function with multiple generics · Issue #10571 · microsoft/TypeScript · GitHub
Open Graph Title: Allow skipping some generics when calling a function with multiple generics · Issue #10571 · microsoft/TypeScript
X Title: Allow skipping some generics when calling a function with multiple generics · Issue #10571 · microsoft/TypeScript
Description: Right now in TypeScript it's all or nothing when calling generic methods. You can either skip typing all the generics altogether and they will be inferred from the context (if possible), or you have to manually (re)define all of them. Bu...
Open Graph Description: Right now in TypeScript it's all or nothing when calling generic methods. You can either skip typing all the generics altogether and they will be inferred from the context (if possible), or you hav...
X Description: Right now in TypeScript it's all or nothing when calling generic methods. You can either skip typing all the generics altogether and they will be inferred from the context (if possible), or you...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/10571
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Allow skipping some generics when calling a function with multiple generics","articleBody":"Right now in TypeScript it's all or nothing when calling generic methods. You can either skip typing all the generics altogether and they will be inferred from the context (if possible), or you have to manually (re)define all of them. But the reality isn't black and white, there are also shades of gray, where we can infer types for some of the generic parameters, but not others. Currently those have to be unnecessarily verbose by forcing the programmer to explicitly restate them.\n\nTake a look at these 3 cases:\n\nCase 1 - everything can be inferred - no need to call the method with `\u003c>` definition:\n\n``` ts\nfunction case1\u003cA, B, C>(a: A, b: B, c: C): A {}\n\nexample(1, '2', true);\n```\n\nCompiler knows that:\n A is number\n B is string\n C is boolean\n\n---\n\nCase 2 - nothing can be inferred, so we need to state what A, B and C should be, otherwise they'll default to `{}`:\n\n``` ts\nfunction case2\u003cA, B, C>(): A {}\n\nexample\u003cnumber, string, boolean>();\n```\n\n---\n\nCase 3 - the one that's interesting to this feature request - some can be inferred, some can't:\n\n``` ts\nfunction case3\u003cA, B, C>(b: string, c: boolean): A {}\n\n// incorrect type of A - left unspecified:\nexample('thing'); \n\n// correct, but unnecessarily verbose - we already know that 'thing' is a string and true is a bool\nexample\u003cnumber, string, boolean>('thing', true);\n```\n\nNow, typing `string, boolean` in the above example isn't a big deal, but with complex scenarios, say with a method using 5 generics, where you can infer 4 of them, retyping them all seems overly verbose and prone to error.\n\nIt would be great if we could have some way to skip re-typing the types that can automatically be inferred. Something like a special `auto` or `inferred` type, so we could write:\n\n``` ts\nexample\u003cnumber, auto, auto>('thing', bool);\n```\n\nOr maybe even, if we only want to specify those up to a certain point:\n\n``` ts\nexample\u003cnumber>('thing', bool);\n```\n\nThe above \"short-hand\" notation could perhaps be different to account for function overloads with different number of generics.\n\nHaving such a feature would solve newcomers encountering problems such as this one: http://stackoverflow.com/questions/38687965/typescript-generics-argument-type-inference/38688143\n","author":{"url":"https://github.com/niieani","@type":"Person","name":"niieani"},"datePublished":"2016-08-26T23:13:24Z","url":"https://github.com/microsoft/TypeScript/issues/10571"}
| 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:8e25ce54-d3e0-a293-371a-a671c1bc167e |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | B718:65478:2861663:3593EBF:6B01AB12 |
| html-safe-nonce | 57f3eb3bf51681e31e767f475fbb464dab9ad891c57ca03703adc4045c65deb0 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCNzE4OjY1NDc4OjI4NjE2NjM6MzU5M0VCRjo2QjAxQUIxMiIsInZpc2l0b3JfaWQiOiIxMjExMDg1OTkzNjM0MDE2MDE5IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | d377979b6e6c8bde8391530384542129cf8864323efc5e8c7987984e5045df80 |
| hovercard-subject-tag | issue:173560819 |
| 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/10571/issue_layout |
| twitter:image | https://opengraph.githubassets.com/b1b5271c737353e7cea28807fde82de1c9e9fb8814be80dcb8de614f7d0e0a98/microsoft/TypeScript/issues/10571 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/b1b5271c737353e7cea28807fde82de1c9e9fb8814be80dcb8de614f7d0e0a98/microsoft/TypeScript/issues/10571 |
| og:image:alt | Right now in TypeScript it's all or nothing when calling generic methods. You can either skip typing all the generics altogether and they will be inferred from the context (if possible), or you hav... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | niieani |
| hostname | github.com |
| expected-hostname | github.com |
| None | cc5dfdd32f62ebd3223cab3af3aa2b268d3605218237326a48a99d1efd89495c |
| 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 | 8c77c9e8439031ad54c75a250694842892f693e1 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width