Title: Inconsistent behavior when inferring a generic type from a function · Issue #52580 · microsoft/TypeScript · GitHub
Open Graph Title: Inconsistent behavior when inferring a generic type from a function · Issue #52580 · microsoft/TypeScript
X Title: Inconsistent behavior when inferring a generic type from a function · Issue #52580 · microsoft/TypeScript
Description: Bug Report 🔎 Search Terms Not sure what to search for, tried various combos of "infer" "generic" "function" but seems too general. 🕗 Version & Regression Information Tried in 4.0.5, 4.6.3, and 4.9.4. ⏯ Playground Link Playground link wit...
Open Graph Description: Bug Report 🔎 Search Terms Not sure what to search for, tried various combos of "infer" "generic" "function" but seems too general. 🕗 Version & Regression Information Tried in 4.0.5, 4.6.3, and 4.9....
X Description: Bug Report 🔎 Search Terms Not sure what to search for, tried various combos of "infer" "generic" "function" but seems too general. 🕗 Version & Regression Informati...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/52580
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Inconsistent behavior when inferring a generic type from a function","articleBody":"# Bug Report\r\n\r\n### 🔎 Search Terms\r\n\r\nNot sure what to search for, tried various combos of \"infer\" \"generic\" \"function\" but seems too general.\r\n\r\n### 🕗 Version & Regression Information\r\n\r\nTried in 4.0.5, 4.6.3, and 4.9.4.\r\n\r\n### ⏯ Playground Link\r\n\r\n[Playground link with relevant code](https://www.typescriptlang.org/play?#code/C4TwDgpgBAYgrgOwMYGcA8BBANFAQlCAD2AgQBMUoAlCJAewCcy0VgGBLBAcx0QGsEdAO4IAfKKgBeKAG8AsACgoUANp8onKHwgg6AMzwBdAFyzFy5XoSmAFAENT2KACNTuAJRSJANzrsyANzmFsAAFpxcbmqGQUpQAL6x8YqKeohIwOx0CFB6dHSYOPhEJOSUNPRMLGwRvAgCwmKiNlYopvDI6E64op4yyQqp+Tbycc52DKajFrnWUPamrBzcnpIS-TgA9JtQaAC0e1AAolRUAPJUUAASJ0fBymERpgDkdihkz1jB8V8K8e6xIZ0EbBcaTMxxSxzBZQJa1FyrdY-KDbXYHKAAOTOx1OF2ut3uUEe3Bebw+v2UP0U-1iQA)\r\n\r\n### 💻 Code\r\n\r\n```ts\r\n// see also playground link\r\n\r\ntype Funcs\u003cA, B extends Record\u003cstring, unknown>> = {\r\n [k in keyof B]: {\r\n fn: (a: A, b: B) => void;\r\n thing: B[k];\r\n };\r\n}\r\n\r\nfunction foo\u003cA, B extends Record\u003cstring, unknown>>(fns: Funcs\u003cA, B>) {}\r\n\r\nfoo({\r\n bar: {\r\n fn: (a: string) => {}, // \u003c-- ERROR HERE\r\n thing: 'asd',\r\n },\r\n});\r\n\r\nfoo({\r\n bar: {\r\n fn: (a: string, b) => {}, // \u003c-- NO ERROR HERE\r\n thing: 'asd',\r\n },\r\n});\r\n```\r\n\r\n### Actual behavior\r\n\r\nIt infers generic `\u003cA>` as `unknown`, in the former case only. It then checks assignability of `unknown` to `a: string` and raises an error.\r\n\r\n```\r\nType '(a: string) => void' is not assignable to type '(a: unknown, b: { bar: string; }) => void'.\r\n Types of parameters 'a' and 'a' are incompatible.\r\n Type 'unknown' is not assignable to type 'string'.(2322)\r\ninput.tsx(3, 5): The expected type comes from property 'fn' which is declared here on type '{ fn: (a: unknown, b: { bar: string; }) => void; thing: string; }'\r\n```\r\n\r\n### Expected behavior\r\n\r\nI didn't expect it to infer `unknown` in the former case, and more generally was surprised that including/excluding `b` from the argument list would affect inference from `a`. I thought it should see `string` in both cases, and that all of the above would compile.\r\n","author":{"url":"https://github.com/greim","@type":"Person","name":"greim"},"datePublished":"2023-02-02T22:15:31Z","url":"https://github.com/microsoft/TypeScript/issues/52580"}
| 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:3f252099-3a34-4799-32da-e787cb1133f9 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | AC70:36EF3:4E7BCE:68027A:6B017961 |
| html-safe-nonce | 984c74fdc7ac50187086df7e757463c270d418d1e7dd7fd805f31aadb34f7f05 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBQzcwOjM2RUYzOjRFN0JDRTo2ODAyN0E6NkIwMTc5NjEiLCJ2aXNpdG9yX2lkIjoiNzMwNTA5NjA3NjM4NDYzMTEzOCIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 5597de6a595a869ad330e125576ced1308bb95b34412b50bf970871ee3f8597d |
| hovercard-subject-tag | issue:1568865641 |
| 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/52580/issue_layout |
| twitter:image | https://opengraph.githubassets.com/a2af971d7ddbc6363945010b3f4fa7638e9dd7604d638e71733d47dd3f1bb9af/microsoft/TypeScript/issues/52580 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/a2af971d7ddbc6363945010b3f4fa7638e9dd7604d638e71733d47dd3f1bb9af/microsoft/TypeScript/issues/52580 |
| og:image:alt | Bug Report 🔎 Search Terms Not sure what to search for, tried various combos of "infer" "generic" "function" but seems too general. 🕗 Version & Regression Information Tried in 4.0.5, 4.6.3, and 4.9.... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | greim |
| 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 | canary-2 |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width