Title: Constraint in generic type forgotten in assignability check · Issue #58370 · microsoft/TypeScript · GitHub
Open Graph Title: Constraint in generic type forgotten in assignability check · Issue #58370 · microsoft/TypeScript
X Title: Constraint in generic type forgotten in assignability check · Issue #58370 · microsoft/TypeScript
Description: 🔎 Search Terms generic type constraint not applied inside type 🕗 Version & Regression Information This changed in commit or PR #57871 ⏯ Playground Link https://www.typescriptlang.org/play/?ts=5.4.5#code/C4TwDgpgBAggdiA0hEUC8U4FcC2AjCAJyg...
Open Graph Description: 🔎 Search Terms generic type constraint not applied inside type 🕗 Version & Regression Information This changed in commit or PR #57871 ⏯ Playground Link https://www.typescriptlang.org/play/?ts=5.4.5...
X Description: 🔎 Search Terms generic type constraint not applied inside type 🕗 Version & Regression Information This changed in commit or PR #57871 ⏯ Playground Link https://www.typescriptlang.org/play/?ts=5...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/58370
X: @github
Domain: github.com
IPv4 address: 140.82.113.3
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Constraint in generic type forgotten in assignability check","articleBody":"### 🔎 Search Terms\r\n\r\ngeneric type constraint not applied inside type\r\n\r\n### 🕗 Version & Regression Information\r\n\r\n- This changed in commit or PR #57871\r\n\r\n### ⏯ Playground Link\r\n\r\nhttps://www.typescriptlang.org/play/?ts=5.4.5#code/C4TwDgpgBAggdiA0hEUC8U4FcC2AjCAJygB8oBnYQgSzgHNSKR8B7AGwG4AoL0SKAEoRgWQnAAq4CMhAsAZgB4A8ngBWUCAA9gEOABNyUFmogBjYAD50UFeq079h46rPAuUD1AD8UANoBrFHkbNQBdDW1dAz84CAA3IlD3TxSfWITCZJSPAC4oAG8-GShaKEDZORD1AENDShp6UIBaLzyACgBKdCtigF9feto6JJS89KJuXikoGQBJOQBlajo4apFCCCU5W1cFLKqIh2jnVwAafeL7KMN4JBRzlKF1iSkZQwwn0RfIGXllNQs5ysH2EX0kPxQhiujignW6MxQ3lhbThaB6KC60OinzE4OkkKRxTG8SIXWJGUmWjALEIwCgfGgQj0WFMED0AEZ-nZIjCTuZTgjUFibggZAKAGrVNhYCAC2xvQ7XMpBSq2azlYK2YEXRHCwXzJYrNaiTbbEzmLkCmQWfapQWKmHygkAemdsDYAHdqiBDFQZQA6W3ZDw+Qq+RAlOCC0J5SXS6C9IPB8lEJNQFOESauqAAdRp-kM1EqphYcHq1VodOohg2TVMAAszIE9JHyNQ9NAGYGqTS6QzBGyWWyAExch3Hc3AK26nnRW5iqBxmVytQKvUa1VqdUqqralKXWciu4gNM+A9HQxzRbLVbrU07C22acgG3B4NnmcXqoK7MwT3e31CADNM31DIpI2jWMpRlKBEzfeCMxAzxEIQzASUzLggA\r\n\r\n### 💻 Code\r\n\r\n```ts\r\ntype AnyKey = number | string | symbol;\r\n\r\ntype ReturnTypeKeyof\u003cObj extends object> = Obj extends object\r\n ? [keyof Obj] extends [never]\r\n ? never\r\n : { [Key in keyof Obj as string]-?: () => Key }[string]\r\n : never;\r\n\r\ntype KeyIfSignatureOfObject\u003c\r\n Obj extends object,\r\n Key extends AnyKey,\r\n ReturnTypeKeys = ReturnTypeKeyof\u003cObj>,\r\n> = ReturnTypeKeys extends () => Key ? ((() => Key) extends ReturnTypeKeys ? Key : never) : never;\r\n\r\nexport type Reduced\u003cObj extends object, Key extends AnyKey, Value, ObjKeys extends keyof Obj = keyof Obj> =\r\n Key extends KeyIfSignatureOfObject\u003cObj, Key>\r\n ? Key extends ObjKeys // Always true.\r\n ? { [K in Key]: Value }\r\n : never\r\n : never;\r\n```\r\n\r\n\r\n### 🙁 Actual behavior\r\n\r\nGives error `Type 'ObjKeys & KeyIfSignatureOfObject\u003cObj, Key, ReturnTypeKeyof\u003cObj>> & Key' is not assignable to type 'string | number | symbol'.`\r\n\r\n### 🙂 Expected behavior\r\n\r\nNo error; `Key` was constrained to `AnyKey` and should be usable as a key.\r\n\r\n### Additional information about the issue\r\n\r\nI reduced this from a much larger example, so the code here doesn't make much sense (the names of the helper types might also not make sense anymore). It might be possible to reduce it further but I wasn't able to find anything obvious.\r\n\r\nAs the playground shows, simply wrapping the body of the type with a `Key extends AnyKey` check makes the error go away, which makes me think that this must be a bug (I guess it distributes the union, but the entire union should be valid as a key type). Checking `Key extends ObjKeys` *before* `KeyIfSignatureOfObject` also makes the error go away.","author":{"url":"https://github.com/ehoogeveen-medweb","@type":"Person","name":"ehoogeveen-medweb"},"datePublished":"2024-04-30T11:21:38Z","url":"https://github.com/microsoft/TypeScript/issues/58370"}
| 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:8c5eb3fb-30e0-29cf-fdbf-2b57d4f1d643 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | B1C2:198B2:42F2C5:592C9D:6B04F3D7 |
| html-safe-nonce | b2b0a457f560c853d5b570d8abc947a19137f9ba87b77cf9fbf277847efe13fd |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCMUMyOjE5OEIyOjQyRjJDNTo1OTJDOUQ6NkIwNEYzRDciLCJ2aXNpdG9yX2lkIjoiODI3OTMzOTE2NzU4NzMwMDMxMSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 6cfbd244a26e8519b210fc21030160ba6a4eb11019b7ccfaf1aaf04dc0deb4a0 |
| hovercard-subject-tag | issue:2271190661 |
| 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/58370/issue_layout |
| twitter:image | https://opengraph.githubassets.com/800fe08f03504525f4d90b73a90ef61ba68012e654caaf73d15865d383d14598/microsoft/TypeScript/issues/58370 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/800fe08f03504525f4d90b73a90ef61ba68012e654caaf73d15865d383d14598/microsoft/TypeScript/issues/58370 |
| og:image:alt | 🔎 Search Terms generic type constraint not applied inside type 🕗 Version & Regression Information This changed in commit or PR #57871 ⏯ Playground Link https://www.typescriptlang.org/play/?ts=5.4.5... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | ehoogeveen-medweb |
| hostname | github.com |
| expected-hostname | github.com |
| None | c0ff704de2671f716381d0ef36115cddb81606dd5397a5b003230bb8c02d623b |
| 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 | cbe60d43d23e17438879ed212ad20068394651c4 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
| Server | github.com |
| Content-Type | text/html; charset=utf-8 |
| Cache-Control | max-age=0, private, must-revalidate |
Viewport: width=device-width