Title: ConditionalRoot.isDistributive is buggy · Issue #44019 · microsoft/TypeScript · GitHub
Open Graph Title: ConditionalRoot.isDistributive is buggy · Issue #44019 · microsoft/TypeScript
X Title: ConditionalRoot.isDistributive is buggy · Issue #44019 · microsoft/TypeScript
Description: Bug Report ConditionalRoot.isDistributive is a flag that indicates that a conditional "distributes" over union. Its set to true when the checkType is a TypeParameter. When its true, two main things happen: getIndexOfMappedType converts k...
Open Graph Description: Bug Report ConditionalRoot.isDistributive is a flag that indicates that a conditional "distributes" over union. Its set to true when the checkType is a TypeParameter. When its true, two main things...
X Description: Bug Report ConditionalRoot.isDistributive is a flag that indicates that a conditional "distributes" over union. Its set to true when the checkType is a TypeParameter. When its true, two m...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/44019
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"ConditionalRoot.isDistributive is buggy","articleBody":"# Bug Report\r\n\r\n`ConditionalRoot.isDistributive` is a flag that indicates that a conditional \"distributes\" over union. Its set to true when the `checkType` is a `TypeParameter`. When its true, two main things happen:\r\n\r\n - `getIndexOfMappedType` converts `keyof { [ K in Keys as DistributiveConditional\u003cK> ] : X }` to `DistributiveConditional\u003cKeys>` which is clearly unsound in many cases (this is the bug reported in #43920).\r\n - `instantiateConditionalType` attempts to undo what it assumes `getIndexOfMappedType` did. This sometimes prevents bugs that `getIndexOfMappedType` would otherwise have introduced, but for other cases can break the conditional.\r\n\r\nFor the first case, here's a simplified version of the example from #43920\r\n\r\n```\r\ntype KeysExtendedBy\u003cT,U> = keyof {[K in keyof T as U extends T[K] ? K :never] : T[K]};\r\ninterface M {\r\n a: boolean;\r\n b: number;\r\n}\r\nfunction f(x : KeysExtendedBy\u003cM,number>) {\r\n return x;\r\n}\r\nf(\"a\"); // \u003c- f should only accept \"b\" as a parameter.\r\n```\r\nHere, `getIndexOfMappedType` optimizes `KeysExtendedBy\u003cT,U>` to `U extends T[keyof T] ? keyof T : never` (which is clearly wrong). And in this case, instantiateConditionalType can't fix it, because it only tries to spread on the checkType.\r\n\r\nThis can be fixed by adding a redundant guard:\r\n\r\n```\r\ntype KeysExtendedBy\u003cT,U> = keyof {[K in keyof T as T[K] extends unknown ? U extends T[K] ? K : never : never] : T[K]};\r\n```\r\n\r\nNow the top level conditional is not `isDistributive`, and we don't call `getIndexOfMappedType`.\r\n\r\nFor the second case:\r\n\r\n```\r\ntype Bug\u003cT, U> = U extends \"a\" ? T[U & keyof T] : never;\r\ninterface M {\r\n a: boolean;\r\n b: number;\r\n}\r\nfunction f(x : Bug\u003cM, \"a\"|\"b\">) {\r\n return x;\r\n}\r\nf(false);\r\n```\r\n\r\nClearly, `\"a\"|\"b\"` does not extend `\"a\"`, so `f`'s parameter type should be `never`. But `instantiateConditionalType` notes that `root.isDistributive` is set (since `U` is a `TypeParameter`), and that `checkType` is a union (the keys of M), and so it replaces the conditional with `(\"a\" extends \"a\" ? M[\"a\"] : never) | (\"b\" extends \"a\" ? M[\"b\"] : never)` which reduces to `M[\"a\"]` which is `boolean`.\r\n\r\nI think these bugs show pretty conclusively that setting `isDistributive` whenever the `checkType` is a `TypeParameter` is broken. Perhaps only doing it when the checkType is a `MappedType`'s type parameter (ie the `K` above) would be safe; but even then I think it often relies on `instantiateConditionalType` to fix it. Consider eg:\r\n\r\n```\r\ntype KeysExtendingLiteral\u003cT> = keyof {\r\n [K in keyof T as K extends \"b\" ? K : never] : T[K];\r\n}\r\ninterface M {\r\n a: boolean;\r\n b: number;\r\n}\r\nfunction f(x: KeysExtendingLiteral\u003cM>) {\r\n return x;\r\n}\r\nf(\"b\");\r\n```\r\n\r\n`KeysExtendingLiteral` gets converted to `keyof T extends \"b\" ? \"b\" & keyof T : never` (you can see this by hovering over KeysExtendingLiteral in line 8 of playground example 3) - which again, is not the same thing. The original should evaluate to \"b\" whenever T has a \"b\" field, and never otherwise. The modified version evaluates to never unless T has a \"b\" field, and no other fields. However, in this case, we *do* get the correct results - because `instantiateConditionalType` converts it back to a union of individual tests - ie `(\"a\" extends \"b\" ? \"b\" & \"a\" : never)|(\"b\" extends \"b\" ? \"b\" & \"b\" : never)`\r\n\r\nMy proposal is to rip out the isDistributive code altogether. The delicate dance where we first transform to an incorrect conditional, hope nobody notices, and then transform it back again at the end seems horribly unsound.\r\n\r\nIf this is really important for type inference, maybe the solution is to only call getIndexOfMappedType when the checkType is the MappedType's type parameter, and then set *another* flag to indicate the transformation was done, and have instantiateConditionalType check *that* flag. I'm not 100% convinced that's sound, but it should be much better than the current situation.\r\n\r\n### 🔎 Search Terms\r\n\r\nisDistributive\r\n\r\nI found #43920 (my own report) which turns out to be a special case of the problems with isDistributive, and #30152 which appears to be a different problem.\r\n\r\n### 🕗 Version & Regression Information\r\n\r\nIts present on master, and has been since at least 4.1.5\r\n\r\n- This is the behavior in every version I tried, and I reviewed the FAQ for entries about it.\r\n\r\n### ⏯ Playground Links\r\n\r\n[Example 1](https://www.typescriptlang.org/play?#code/C4TwDgpgBA0hIGcCiAPYEB2ATCWBCIAPACoA0AqgHxQC8UA1vAPYBmUA3gNoxQCWGDZm2JQAhgijkoENJiwTi3ALpQA-LCgAuDBABuEAE4rNURTCUBfANwBYAFD90BlqIDG0ALId7UMSYBGTEwANhCiGLZ2vv4mGACuALb+hpEW9ixxGK7AvEwCLAAUKFqw8Miy2LgEhB6k8UmGlACU3lFQBhDAcQYCKKnpBQBEooNNkUA)\r\n[Example 2](https://www.typescriptlang.org/play?#code/C4TwDgpgBAQgrgcwDwBUA0UCqA+KBeLKCAD2AgDsATAZygCIBDOqAfihQG1MoAyKAawggA9gDN2AXSgAuKOQgA3CACcA3AFgAUAEtyZZaIYBjaAFkoAby1QoDWQCNhwgDYQG5DZpv3Z5OAFt7FU8AXy1ROHIjYG1hcihRAApiGVhEJFMMRjoAHzp7OmwASktrKGUIYDhleOJQ8MTDZ2oIItUgA)\r\n[Example 3](https://www.typescriptlang.org/play?#code/C4TwDgpgBA0hIGcCiAPYEB2ATAlhg5gDI7oBOAhgDYA8AKgHxQC8UA1vAPYBmUA3gLAAoKFADaMKHjacetKOQSwoENJiyKARACMNUAPxKAXFAwQAbhFIBdKMdrirAbiEBfIXjJdyAY2gBZPiERcmMtDg5KCHIMZ2EoLWMMAFcAWy1LWLdBLiSMb2AcDgwoLgAKFGM4RFR0bDwiEksqaj96AEpAuNIIYCTSYpRMoTLtDTbYoA)\r\n\r\n### 💻 Code\r\n\r\n```\r\ntype KeysExtendedBy\u003cT,U> = keyof {[K in keyof T as U extends T[K] ? K : never] : T[K]};\r\ninterface M {\r\n a: boolean;\r\n b: number;\r\n}\r\nfunction f(x : KeysExtendedBy\u003cM,number>) {\r\n return x;\r\n}\r\nf(\"a\");\r\n```\r\n\r\n### 🙁 Actual behavior\r\n\r\nThe call to f(\"a\") is deemed correct\r\n\r\n### 🙂 Expected behavior\r\n\r\nIt should fail because `number` does not extend `M[\"a\"]` which is boolean, so \"a\" should not be included in the keys of the mapped type (and note that it *isn't* included in the mapped type itself).","author":{"url":"https://github.com/markw65","@type":"Person","name":"markw65"},"datePublished":"2021-05-09T23:09:55Z","url":"https://github.com/microsoft/TypeScript/issues/44019"}
| 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:3cffbfae-f234-a002-c3d0-1cfa1d20ff37 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 8600:38E318:436E2CF:5516823:6B026255 |
| html-safe-nonce | 9e8bf3d51ed0ec2792b5e913bb483f4f5d34cc94759c4a54b7f2630749864006 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4NjAwOjM4RTMxODo0MzZFMkNGOjU1MTY4MjM6NkIwMjYyNTUiLCJ2aXNpdG9yX2lkIjoiNTMyNDQ5MDg5NTg4NDcwNjM4OSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 9b529f8e22724b28326b7407aa6b615f54d420ae4ca163fc3633685587122759 |
| hovercard-subject-tag | issue:882947035 |
| 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/44019/issue_layout |
| twitter:image | https://opengraph.githubassets.com/ea0a6ab7e3d1760b7edd160e1d6eda50be04b7b670ea63f8b4100a4fd397515f/microsoft/TypeScript/issues/44019 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/ea0a6ab7e3d1760b7edd160e1d6eda50be04b7b670ea63f8b4100a4fd397515f/microsoft/TypeScript/issues/44019 |
| og:image:alt | Bug Report ConditionalRoot.isDistributive is a flag that indicates that a conditional "distributes" over union. Its set to true when the checkType is a TypeParameter. When its true, two main things... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | markw65 |
| hostname | github.com |
| expected-hostname | github.com |
| None | 87e774ee7c6b3bee0490394e4d3e57ec9dbc956f74b87ea685dc8996c1676456 |
| 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 | 423dbb54c5e7bfecc838eb387c1435eb3d0e760d |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width