Title: Transformers in a map generated via a Mapped Type with Key Remapping are erroneously inferred to return `any` · Issue #57097 · microsoft/TypeScript · GitHub
Open Graph Title: Transformers in a map generated via a Mapped Type with Key Remapping are erroneously inferred to return `any` · Issue #57097 · microsoft/TypeScript
X Title: Transformers in a map generated via a Mapped Type with Key Remapping are erroneously inferred to return `any` · Issue #57097 · microsoft/TypeScript
Description: 🔎 Search Terms "mapped types", "key remapping", "indexed access type" 🕗 Version & Regression Information This is the behavior in every version I tried (5.2.2, 5.3.2, 5.4.0-dev.20240119), and I reviewed the FAQ. ⏯ Playground Link https://...
Open Graph Description: 🔎 Search Terms "mapped types", "key remapping", "indexed access type" 🕗 Version & Regression Information This is the behavior in every version I tried (5.2.2, 5.3.2, 5.4.0-dev.20240119), and I revi...
X Description: 🔎 Search Terms "mapped types", "key remapping", "indexed access type" 🕗 Version & Regression Information This is the behavior in every version I tried (5.2.2, 5.3....
Opengraph URL: https://github.com/microsoft/TypeScript/issues/57097
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Transformers in a map generated via a Mapped Type with Key Remapping are erroneously inferred to return `any`","articleBody":"### 🔎 Search Terms\n\n\"mapped types\", \"key remapping\", \"indexed access type\"\n\n### 🕗 Version & Regression Information\n\n- This is the behavior in every version I tried (5.2.2, 5.3.2, 5.4.0-dev.20240119), and I reviewed the FAQ.\n\n### ⏯ Playground Link\n\nhttps://www.typescriptlang.org/play?target=99&ts=5.4.0-dev.20240119#code/KYDwDg9gTgLgBDAnmYcByEAmwCSA7GYKAMwEMBjYAHgBkBLQqUgGwBVlVRC9MBnOXjCh08AczgBeAUJGiAfJLgBvAFBx1CDgC449Ri3YoA3CoC+JlaEixNKOAGUidFnQBewTPZliAssBikigBKwOTQmFSCwmIANNLR4gA+cACuPMDEIh5yFlbQ8Eh2ACIQALYAoiBCFISYGNhFpAGKqhpweFjAhsA6UbImbaX+pDqOwi7unt6ifgEm5iqW4Pm2qPVdECWlY87MbkRU63BcwDz86-iMZJQKUgAUahod2DrrMSoAlJIKOxMeXglZqRcssbIU1p0gb89u4oD5SGBDp0AKp4OgQPDHKqnPjoTqXIjXYC3ZSPdQAbSOIjx2FR6MxpHOnXJAHJwSyALoc16dVibMrQ-ZQJHYHJmCxhPCCOAAC0ZiiorCx3FxEAARgArUIwOQPNoQHSsd5tADWvWmcGSeBSpTVREtAkQtogzHeHx0JrgdH4JuAiAgxDgSokCgA8prtQA6OW8UMAdzwdwgcRNHwlGOlQkQgvcyN4snhYDAsgVSpOZxpuAIhIoxL1GlKCOLYh52ChTj+cIRirkxqenUNbu+pLadEDdxjd0bRdkcWewEj4I+X1abQ0kulgSk0+bonJ88XHA5FjX64z8DVilId3nacWp-UUH8KSgDJvnTvbVM940MBlUAgON2mAIDunKKAAKgO4WVRXgUiLfIPHaTpVhZO9zCAA\n\n### 💻 Code\n\n```ts\r\nexport type NodeInterface\u003cLiteralType extends string = string> = {\r\n type: LiteralType;\r\n};\r\n\r\nexport type SerializedStringMeta = Record\u003cstring, string | undefined>;\r\n\r\nexport type DomExtractedNodeData = {\r\n nodeType: string;\r\n meta: SerializedStringMeta;\r\n};\r\n\r\nexport type NodeToDomSerializer\u003cNode extends NodeInterface> = (\r\n node: Node,\r\n) => SerializedStringMeta;\r\n\r\nexport type NodeMetaSerializerMap\u003cNodeUnion extends NodeInterface> = {\r\n [Node in NodeUnion as Node['type']]: NodeToDomSerializer\u003cNode>;\r\n};\r\n\r\nconst has = \u003cT extends object>(\r\n o: T,\r\n k: string | number | symbol,\r\n): k is keyof T => Object.hasOwn(o, k);\r\n\r\nconst trySerializeUsingMapping = \u003cT extends NodeInterface>(\r\n mapping: NodeMetaSerializerMap\u003cT>,\r\n node: T,\r\n) => {\r\n if (has(mapping, node.type)) {\r\n const a = mapping[node.type];\r\n\r\n const b = a(node);\r\n\r\n return a(node);\r\n }\r\n\r\n throw new TypeError('Unsupported node type');\r\n};\r\n```\r\n\n\n### 🙁 Actual behavior\n\nType of the variable `a` is correctly inferred to be `NodeMetaSerializerMap\u003cT>[keyof NodeMetaSerializerMap\u003cT>]`. To be rigorous this should be `NodeToDomSerializer\u003cT>`.\r\n\r\nType of variable `b`, however, is inferred to be `any`. Compiler doesn't warn me about this, and as you can understand, this could lead to all sorts of potential `any`-leakages. Compiler option `strict` doesn't change this as well.\n\n### 🙂 Expected behavior\n\nI expect type of the variable `b` to be correctly inferred as `SerializedStringMeta`, or at least some kind of error to be shown in the call site of `a(node)`.\n\n### Additional information about the issue\n\nI've also reviewed [this](https://github.com/microsoft/TypeScript/pull/47109) pull request. I've tried the following work-around:\r\n\r\n```typescript\r\ntype TypeToNodeMapping\u003cNode extends NodeInterface> = {\r\n [K in Node as Node['type']]: Node;\r\n};\r\n\r\nexport type NodeMetaSerializerMap\u003cNodeUnion extends NodeInterface> = {\r\n [K in keyof TypeToNodeMapping\u003cNodeUnion>]: NodeToDomSerializer\u003c\r\n TypeToNodeMapping\u003cNodeUnion>[K]\r\n >;\r\n};\r\n```\r\nThis results in a following error being shown at the call site for `a(node)`:\r\n\r\n\u003cimg width=\"604\" alt=\"image\" src=\"https://github.com/microsoft/TypeScript/assets/1677844/07e2c227-0ebd-4d67-9f51-c7316d49d6e0\">\r\n\r\nThis also seems like a weird behaviour, because I've tested different things and verified that `TypeToNodeMapping\u003cNodeInterface>[NodeInterface['type']]` is correctly resolved as `NodeInterface`. Seems to me like the compiler is for some reason refusing to unpack the types further, despite the fact it seems safe to use the upper boundary `NodeInterface` when resolving types for `T`.\r\n","author":{"url":"https://github.com/brotheroftux","@type":"Person","name":"brotheroftux"},"datePublished":"2024-01-19T16:18:51Z","url":"https://github.com/microsoft/TypeScript/issues/57097"}
| 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:c83b1ca8-a553-7124-c173-6e237f556d68 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 86D2:3E1BDD:239A4:2FC51:6B017BC4 |
| html-safe-nonce | 80b0f21a707125c03b476745424ba6bcf4a1ea3b0a496ad16d86e2bdf68fba01 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4NkQyOjNFMUJERDoyMzlBNDoyRkM1MTo2QjAxN0JDNCIsInZpc2l0b3JfaWQiOiI2MDE2MzE5NTMzNTI1MDcyODM2IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 046032a3712b0e3e288e0d568c9fec5ba0b773d68a871f0fc832711196cd8c90 |
| hovercard-subject-tag | issue:2090868120 |
| 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/57097/issue_layout |
| twitter:image | https://opengraph.githubassets.com/b64ddbf7abb84707c10dd89280d4688eddefeade8fb0b85dea4fa32efb4346ed/microsoft/TypeScript/issues/57097 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/b64ddbf7abb84707c10dd89280d4688eddefeade8fb0b85dea4fa32efb4346ed/microsoft/TypeScript/issues/57097 |
| og:image:alt | 🔎 Search Terms "mapped types", "key remapping", "indexed access type" 🕗 Version & Regression Information This is the behavior in every version I tried (5.2.2, 5.3.2, 5.4.0-dev.20240119), and I revi... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | brotheroftux |
| 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