Title: Incorrect optimization for keyof Mapped type · Issue #43920 · microsoft/TypeScript · GitHub
Open Graph Title: Incorrect optimization for keyof Mapped type · Issue #43920 · microsoft/TypeScript
X Title: Incorrect optimization for keyof Mapped type · Issue #43920 · microsoft/TypeScript
Description: I have this Mapped type which creates a type with just those fields of T that are extended by B. It seems to work. type FieldsExtendedBy
Open Graph Description: I have this Mapped type which creates a type with just those fields of T that are extended by B. It seems to work. type FieldsExtendedBy
X Description: I have this Mapped type which creates a type with just those fields of T that are extended by B. It seems to work. type FieldsExtendedBy<T,B> = {[K in keyof T as (B extends T[K] ? K :never)] ...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/43920
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Incorrect optimization for keyof Mapped type","articleBody":"I have this Mapped type which creates a type with just those fields of T that are extended by B. It seems to work.\r\n\r\n```type FieldsExtendedBy\u003cT,B> = {[K in keyof T as (B extends T[K] ? K :never)] : T[K]};```\r\n\r\nBut this one, to extract the keys of those fields, doesn't:\r\n\r\n```type KeysExtendedByFails\u003cT,B> = keyof {[K in keyof T as (B extends T[K] ? K :never)] : T[K]};```\r\n\r\nKeysExtendedByFails always includes all or none of the keys of the original T. I've setup a playground [here](https://www.typescriptlang.org/play?ts=4.2.3#code/LAKALgngDgpgBAMQJYwDYBMDOBRAHmGAO3RnQCEIAeAFQBoyA+OAXjgG8BtAaTiULgDWMCAHsAZnGpwAhpjgAKMnBj4iWSdwC6cAPxweALkIwAbjABOASm0GNXTQF8A3KFCRY+4TlXFSFAOoi5gKYNPRMrEKiEpw8fILC4pIycvLUWso+6gCuhAKEIgDu-HqKmQTEcun2uvpwRqYWlrbGZlY2do4u4NDwXF54FSTkEAjSSKihdIwsCdHs3Lz8UUlSsgpKKkNVGXqGrU0d1V2uIHwE5mLSAMbwAILscLj1cITZALYARhZOcA6g5wsV1ucCUbDgEBemDA5j4AHNfv8zoQLsD4ABhcpqORguAALx0tk+IhEqBg0kIiNOgMuN3gAFlHtJbHdfp8XmRftcXpikaAxLlrmAkCJ+HCSeh5NlbP0IN4hn4IIFgqF6bRQQxLFCYfD2KA4HBzDAwNlzPxst0kQLCEKRfxPtJJdLPHLBmpFWMJqr1YwtbZobDCHC9SADUaTWa4BbQHyQAB6ONwMAACyQclF8CuXrgorg70hfGh0lQqGkwtzSQALAA6ABM1YAzOrPtkwHBCkEQksk8n4FBSxA4eYRLl0PzBeX+B2VQB9Pgz-vSQfD0dS2wrCTINBYN2+EaUNW+7WB4NsfWG42m82W07W2256chGcUiCFJdrxAoDDy937w+al5cnyIp+DPUMLwja8Y1vCc7TgB1zHkLVwXPcMrzgDhzwNcURElAAiaQ8MsdUEzgLNJhSTJYCFUhq1BOB0BEGA5AKNstjUOA7iwuAcPwz4iNobjePkPDrgE7iHXwwjiLgUjchUaiCHQVBIUfTA6KURjmNeEQ2KyTiJMdET+OIwz8LE0zwINNS50IBcByHEdiBE6SSMTa4giNIUVLI8YKPiRdlyc9Bm1bHS2zEII83gQokBTOAa3rBtuJs+dAsc1c8JMwSrPbTtMFs+ylwy5zRPE8DNBvEBxxtSd4OkPEkJDMNL0jTDcpsl83wgeQ2GZNhcAMStawcWhPgMNgIAMLKGrw0brgmvEDBhbIYFoKaZvMOaHBk0jyLkdYFJgGj0A0himJY3SsWIAyOvy59CFfd8+omwbhocHa3N87NDtwRTaLgek00wXV2QpdA4GudVwbgVA4osYs4EwZMRwwcK4GTaQzBkFL7q657xsm6aHTxObaAWtglpWtaNoQ7bdsTNTz00GMgA), and hovering over the use of KeysExtendedByFails in the function bad in the playground example shows the definition has been incorrectly transformed to:\r\n\r\n```type KeysExtendedByFails\u003cT, B> = B extends T[keyof T] ? keyof T : never```\r\n\r\nwhich is obviously wrong.\r\n\r\nI have another definition, \r\n\r\n```type KeysExtendedByWorks\u003cT,B> = keyof {[K in keyof T as (T[K] extends unknown ? (B extends T[K] ? K :never): never)] : T[K]};```\r\n\r\nwhich works - although `T[K] extends unknown` is pretty much a no op.\r\n\r\nAlso applying keyof to FieldsExtendedBy seems to work in the playground (versions 4.1.5 and later), but fails for me with 4.2.3. I assume there's a setting which is affecting whether it works or not.\r\n\r\n\r\n\r\n","author":{"url":"https://github.com/markw65","@type":"Person","name":"markw65"},"datePublished":"2021-05-01T17:13:41Z","url":"https://github.com/microsoft/TypeScript/issues/43920"}
| 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:ba09b648-b00d-3280-6b1d-81b9b36b9c5c |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | EB68:4BB79:4B8B46C:6035E5D:6B026B49 |
| html-safe-nonce | 14c5fa44a18a42b30b608804928d6312051a2b3306976116d05eddc1d9f34c1c |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFQjY4OjRCQjc5OjRCOEI0NkM6NjAzNUU1RDo2QjAyNkI0OSIsInZpc2l0b3JfaWQiOiI4NzQwMTExNDAyMzYwMDA3NDk3IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 775f8b1b2d51ce299f08111379d99b68f415c17015ef70f01ed8c76161c37427 |
| hovercard-subject-tag | issue:873724974 |
| 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/43920/issue_layout |
| twitter:image | https://opengraph.githubassets.com/c96842ebdb2136baa4963ecf8f19636916464ecff69ae3c9b038f86e78171d0a/microsoft/TypeScript/issues/43920 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/c96842ebdb2136baa4963ecf8f19636916464ecff69ae3c9b038f86e78171d0a/microsoft/TypeScript/issues/43920 |
| og:image:alt | I have this Mapped type which creates a type with just those fields of T that are extended by B. It seems to work. type FieldsExtendedBy |
| 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