Title: Generic type arguments not inferred, default to {} · Issue #5884 · microsoft/TypeScript · GitHub
Open Graph Title: Generic type arguments not inferred, default to {} · Issue #5884 · microsoft/TypeScript
X Title: Generic type arguments not inferred, default to {} · Issue #5884 · microsoft/TypeScript
Description: I sometimes run into scenarios where it's clear to me what a generic type argument should be, but typescript fails to infer it. I think this is because inference only looks at the call site, and not the context. The example I just ran in...
Open Graph Description: I sometimes run into scenarios where it's clear to me what a generic type argument should be, but typescript fails to infer it. I think this is because inference only looks at the call site, and no...
X Description: I sometimes run into scenarios where it's clear to me what a generic type argument should be, but typescript fails to infer it. I think this is because inference only looks at the call site, an...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/5884
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Generic type arguments not inferred, default to {}","articleBody":"I sometimes run into scenarios where it's clear to me what a generic type argument should be, but typescript fails to infer it. I think this is because inference only looks at the call site, and not the context. The example I just ran into was this:\n\n``` typescript\nfunction comparing \u003cA,B> (f : (x : A) => B) : (x : A, y : A) => number {\n return function (x : A, y : A) : number {\n return f(x) \u003c f(y) ? -1 : x == y ? 0 : 1;\n }\n}\n\nvar x : Array\u003c{ foo : string }> = [{ foo : \"hello\" }, { foo : \"world\"}];\n\n// Works, explicit types\nfunction getFoo (o : { foo : string }) : string { return o.foo }\nx.sort(comparing(getFoo))\n\n// Works, inferred any everywhere\nfunction getFoo2 (o) { return o.foo }\nx.sort(comparing(getFoo2))\n\n// Doesn't work, infers type parameters to {}\nx.sort(comparing(o => o.foo))\n```\n\nIn the last case it infers type parameter `A` to `{}`, causing it to fail since `{}` has no property `foo`. But from the context (`comparing` should return a function `(x : { foo : string }, y : { foo : string }) : number)` because it is used as an argument to `sort` on `x`) it should be clear that `A` should be `{ foo : string }`.\n\nIs there any way this kind of inference could be added, or is this too complicated due to overloads/subtyping/etc.\n\nP.S. There seem to be several issues about things like this, but they all seem slightly different. If they're not, my apologies. I'd like to have a place where I can track this issue, so I would appreciate any pointers to another ticket, a FAQ, etc.\n","author":{"url":"https://github.com/hesselink","@type":"Person","name":"hesselink"},"datePublished":"2015-12-02T11:36:17Z","url":"https://github.com/microsoft/TypeScript/issues/5884"}
| 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:9ab4b9c5-6bd1-793d-8bae-6ece30e6a338 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | A93E:26ECEB:21C6145:2AEA217:6B0162DC |
| html-safe-nonce | 881b1bdd1a3bcac3629d3adbfea91013656ebf37adca12c5590c6315dbfa0e7b |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBOTNFOjI2RUNFQjoyMUM2MTQ1OjJBRUEyMTc6NkIwMTYyREMiLCJ2aXNpdG9yX2lkIjoiODgwMzc4MDE2OTM1NDUzNTY0NCIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 64434dda4ae4302d901f3ee7032af72f22e5fbc1d7194a5e2ed93fc34bbd7182 |
| hovercard-subject-tag | issue:119923536 |
| 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/5884/issue_layout |
| twitter:image | https://opengraph.githubassets.com/91e739c1b5ad8d46cbe82b3b71a46d12a263ca2e27c3a06011abb4006421cd47/microsoft/TypeScript/issues/5884 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/91e739c1b5ad8d46cbe82b3b71a46d12a263ca2e27c3a06011abb4006421cd47/microsoft/TypeScript/issues/5884 |
| og:image:alt | I sometimes run into scenarios where it's clear to me what a generic type argument should be, but typescript fails to infer it. I think this is because inference only looks at the call site, and no... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | hesselink |
| 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