Title: Reverse mapped type with a circular type param sometimes not treated as partially inferrable · Issue #48798 · microsoft/TypeScript · GitHub
Open Graph Title: Reverse mapped type with a circular type param sometimes not treated as partially inferrable · Issue #48798 · microsoft/TypeScript
X Title: Reverse mapped type with a circular type param sometimes not treated as partially inferrable · Issue #48798 · microsoft/TypeScript
Description: Bug Report 🔎 Search Terms reverse mapped types, circular type, partially inferrable ⏯ Playground Link Playground link with relevant code 💻 Code type AnyFunction = (...args: any[]) => any; type InferNarrowest
Open Graph Description: Bug Report 🔎 Search Terms reverse mapped types, circular type, partially inferrable ⏯ Playground Link Playground link with relevant code 💻 Code type AnyFunction = (...args: any[]) => any; type Infe...
X Description: Bug Report 🔎 Search Terms reverse mapped types, circular type, partially inferrable ⏯ Playground Link Playground link with relevant code 💻 Code type AnyFunction = (...args: any[]) => any; type I...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/48798
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Reverse mapped type with a circular type param sometimes not treated as partially inferrable","articleBody":"# Bug Report\r\n\r\n### 🔎 Search Terms\r\n\r\nreverse mapped types, circular type, partially inferrable\r\n\r\n### ⏯ Playground Link\r\n\r\n[Playground link with relevant code](https://www.typescriptlang.org/play?ts=4.6.2#code/C4TwDgpgBAggdiAYgVzgY2ASwPZygXigAoA6MgQwCcBzAZwC4pyEBtAXQEoCA+JhAbgBQg0JCgBJOADMIlAHJVK2AO4RawADwAVXoS1QIAD2AQ4AE1p8QgqFAD8UfUZPnL8JKgw44N2-ce+toxOxqYWUNgARgBWEBiB-pIy8ooqasAA8jFxmjoJwb6McBAAbrJCIuDQSbIKlEqq6lmxGNq6UADevpQQ5Ga4ADYgUCwA0lCYeADWECDYUo5sjDUp9Wnq2mNs3EIAvhWi0ADCuFKY1NoA4gNR5AMANI4AysDkJgRQAApKYFc3kXdHgAidRvNRA7jtLq2UEmBidBIsF5g0azCbTWbzZ6vExLBF+Py4OyMDr7BJk2xk3ZQABk+PRmCwd0YMzmCy0yJMewOVS+P20j1G7XGzjCllZWP0Di0WygRVK5WEaFw6igaB6YIAsuQ0AALSbQQjaE7Sc4GUKuKAms4XLTW86Qoi+AD6ytN1GW0lqqUamWyrTtpwdgg4jBK2EwZh4nTJgnVvRM2r1BqI0IZTIGjCBkHMk2oQPuvlhahJCRzZjzpYJtlwVertn6xSIXDT9dsPWAyEoeCBcGwABE3uQgUI21BdoX6xPyZPx4XdhwhEA)\r\n\r\n### 💻 Code\r\n\r\n```ts\r\ntype AnyFunction = (...args: any[]) => any;\r\n\r\ntype InferNarrowest\u003cT> = T extends any\r\n ? T extends AnyFunction\r\n ? T\r\n : T extends object\r\n ? InferNarrowestObject\u003cT>\r\n : T\r\n : never;\r\n\r\ntype InferNarrowestObject\u003cT> = {\r\n readonly [K in keyof T]: InferNarrowest\u003cT[K]>;\r\n};\r\n\r\ntype Config\u003cTGlobal, TState = Prop\u003cTGlobal, \"states\">> = {\r\n states: {\r\n [StateKey in keyof TState]: {\r\n on?: {};\r\n };\r\n };\r\n} & {\r\n initial: keyof TState;\r\n};\r\n\r\ntype Prop\u003cT, K> = K extends keyof T ? T[K] : never;\r\n\r\nconst createMachine = \u003cTConfig extends Config\u003cTConfig>>(\r\n _config: InferNarrowestObject\u003cTConfig>\r\n): void => {};\r\n\r\ncreateMachine({\r\n initial: \"pending\",\r\n states: {\r\n pending: {\r\n on: {\r\n done() {\r\n return \"noData\";\r\n },\r\n },\r\n },\r\n },\r\n});\r\n\r\n```\r\n\r\n### 🙁 Actual behavior\r\n\r\nInferred signature is:\r\n```ts\r\nconst createMachine: \u003cConfig\u003c{\r\n initial: \"pending\";\r\n states: unknown;\r\n}, unknown>>(_config: InferNarrowestObject\u003cConfig\u003c{\r\n initial: \"pending\";\r\n states: unknown;\r\n}, unknown>>) => void\r\n```\r\n\r\n### 🙂 Expected behavior\r\n\r\n`states: unknown` shouldn't be inferred here, the reverse mapped type should be able to infer an object literal type~. The type param should be inferred as something like:\r\n```ts\r\nconst createMachine: \u003c{\r\n initial: \"pending\";\r\n states: {\r\n pending: {\r\n on: {\r\n done: () => \"noData\";\r\n };\r\n };\r\n };\r\n}>(_config: InferNarrowestObject\u003c{\r\n initial: \"pending\";\r\n states: {\r\n pending: {\r\n on: {\r\n done: () => \"noData\";\r\n };\r\n };\r\n };\r\n}>) => void\r\n```\r\n\r\nWe can simplify the repro a little bit, but it will then yield an error at a different position and I can't verify right now if the underlying issue is exactly the same in this case (although from what it looks like the root cause is super similar):\r\n[TS playground without conditional type applied at the argument position](https://www.typescriptlang.org/play?ts=4.6.2#code/C4TwDgpgBAwg9gOwGYEsDmAeAKgcQDZwBGAhngDRRYDKwxw0AvFAAoBOcY2+RpFARAGda9AXwB8YqEwDeAKChQhdCAIBcUOQoUBtGsoDSEEFBQIoAayNwklPfQC66zVoWIA-E4C+AbnlaffgGeUABkGn6mKMAopOqWINa2whC+AbKgkCzsnFgU+pJM+lAQAB70CAAmAhZWNlhQbpTa+vZQ6ggQAG4QrL6yAMaIQlD9rBDKALLE-QAWpoxQ2PDI6MVlEJXVy6iYWNvoEgAUfgD6gyto6nuIO7IAlOqdcCgVUpLSaaPj9FOz84fOSLRWJQPiQSqmNB8Mh+JQiJx+BTgiqQhEuVwINHohQVRAQQ53cLYlxjYAAV1YZj4CDgABE6MQ+L5iVBPDDsWzEaz2dzZJ47r4gA)\r\n\r\nNote that we can fix both playground by using an arrow function instead of a method (probably related to a possible \"hidden\" `this` type param that makes this context sensitive in the case of a method). \r\n\r\nThe first one can be fixed by adding a dummy property to the object containing a method (this makes the object partially inferrable):\r\n[TS playground with a dummy property added](https://www.typescriptlang.org/play?ts=4.6.2#code/C4TwDgpgBAggdiAYgVzgY2ASwPZygXigAoA6MgQwCcBzAZwC4pyEBtAXQEoCA+JhAbgBQg0JCgBJOADMIlAHJVK2AO4RawADwAVXoS1QIAD2AQ4AE1p8QgqFAD8UfUZPnL8JKgw44N2-ce+toxOxqYWUNgARgBWEBiB-pIy8ooqasAA8jFxmjoJwb6McBAAbrJCIuDQSbIKlEqq6lmxGNq6UADevpQQ5Ga4ADYgUCwA0lCYeADWECDYUo5sjDUp9Wnq2mNs3EIAvhWi0ADCuFKY1NoA4gNR5AMANI4AysDkJgRQAApKYFc3kXdHgAidRvNRA7jtLq2UEmBidBIsF5g0azCbTWbzZ6vExLBF+Py4OyMDr7BJk2xk3ZQABk+PRmCwd0YMzmCy0yJMewOVS+P20j1G7XGzjCllZWP0Di0WygRVK5WEaFw6igaB6YIAsuQ0AALSbQQjaE7Sc4GUKuKAms4XLTW86Qoi+AD6ytN1GW0lqqUamWyrTtpwdgg4jBK2EwZh4nTJgnVvRM2r1BqI0IZTIGjCBkHMk2oQPuvlhahJCRzZjzpYJtlwVertjhwEYABYAEyF+u2frFIhcNOdqA9YDISh4IFwbAAETe5CBQgHuw71cX5KXK92HCEQA)\r\n\r\nEspecially given that a dummy property fixes the problem it looks like a weird design limitations.\r\n\r\nWhat I've learned when debugging this:\r\n1. methods + functions with arguments are context sensitive and in `checkFunctionExpressionOrObjectLiteralMethod` they return `anyFunctionType`\r\n2. `anyFunctionType` has `ObjectFlags.NonInferrableType` on it\r\n3. this flag is \"propagating\" and thus is set on the \"parent\" object\r\n4. `uninstantiatedType` for the `on` property of this argument gets computed to `{}` (so it's empty~)\r\n5. and thus it doesn't pass `isPartiallyInferableType` check when resolving the structure of the reverse mapped type, since the object has `ObjectFlags.NonInferrableType` on it AND there are no other properties that would be treated as partially inferrable\r\n6. since no structure is resolved for this mapped type the `unknown` is returned for the `states` property\r\n7. this in turn makes `initial` property to error because `keyof unknown` is `never`\r\n\r\nwhile a workaround is \"known\" here (we've learned this hard way though)~ the whole thing still has some problems because we can't provide a param type for the arrow functions contained in `on` property because once we add any unannotated params to an arrow function it becomes context-sensitive and results in a similar problem:\r\n[TS playground with context-sensitive arrow function](https://www.typescriptlang.org/play?ts=4.6.2#code/C4TwDgpgBAggdiAYgVzgY2ASwPZygXigAoA6MgQwCcBzAZwC4pyEBtAXQEoCA+JhAbgBQg0JCgBJOADMIlAHJVK2AO4RawADwAVXoS1QIAD2AQ4AE1p8QgqFAD8UfUZPnL8JKgw44N2-ce+toxOxqYWUNgARgBWEBiB-pIy8ooqasAA8jFxmjoJwb6McBAAbrJCIuDQSbIKlEqq6lmxGNq6UADevpQQ5Ga4ADYgUCwA0lCYeADWECDYUo5sjDUp9Wnq2mNs3EIAvhWi0ADCuFKY1NoA4gNR5AMANI4AysDkJgRQAApKYFc3kXdHgAidRvNRA7jtLq2UEmBidBIsF5g0azCbTWbzZ6vExLBF+Py4OyMABKcWwlDMGnUlEm1EeRCo1EYNLpXHwvFZcGoOwS+18-N2UAAZPj0ZgsHdGDM5gstMiTHsDlUvj9tI9Ru1xs4wpYZVj9A4tFsoEVSuVhGhcOooGgemCALLkNAAC0m0EI2hO0nOBlCrig3rOFy0QfOkKIvgA+lafcyJNJaqlGplsq1Q6dw4IOIwSthMGYeJ1+YI7b0TE7Xe6iNDxZKBowgZBzHSgfdfLC1Ixa7Zm2Y6d2ErZcIOCQT+sVGIyaOzeD2x7YesBkJQ8EC4NgACJvchAoQL2y7dsLo9849QU+7DhCIA)\r\n\r\nRelated to https://github.com/microsoft/TypeScript/issues/40439","author":{"url":"https://github.com/Andarist","@type":"Person","name":"Andarist"},"datePublished":"2022-04-21T10:23:46Z","url":"https://github.com/microsoft/TypeScript/issues/48798"}
| 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:100cf317-f0e8-1d13-3ae6-a62c884db6d9 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | E3AA:2270F4:BC0918:FBAEFD:6B00F528 |
| html-safe-nonce | 79b3b10c472d6bcd340b6b96a7969f19a867ee718dbabd3de40b901b6aa38eb7 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFM0FBOjIyNzBGNDpCQzA5MTg6RkJBRUZEOjZCMDBGNTI4IiwidmlzaXRvcl9pZCI6IjQ1ODI2NDQzNzA0OTAwNjIxMjAiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | cfe2729decbe712a46f74262508f2a5d4a82046866a6c8e352df9785ff3e3fad |
| hovercard-subject-tag | issue:1210827062 |
| 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/48798/issue_layout |
| twitter:image | https://opengraph.githubassets.com/2dea5d3dfd1abdf1881b973f69b5cc200310e7cb104f5023fea9de4a3f71db2f/microsoft/TypeScript/issues/48798 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/2dea5d3dfd1abdf1881b973f69b5cc200310e7cb104f5023fea9de4a3f71db2f/microsoft/TypeScript/issues/48798 |
| og:image:alt | Bug Report 🔎 Search Terms reverse mapped types, circular type, partially inferrable ⏯ Playground Link Playground link with relevant code 💻 Code type AnyFunction = (...args: any[]) => any; type Infe... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | Andarist |
| hostname | github.com |
| expected-hostname | github.com |
| None | 4ef498cbabcbe3c50befc7a5b743e7072ee770b4daf0e15e54801180d1fa91fc |
| 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 | 9983dd5da06943f833c971dd5b6bc988e2858be5 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width