Title: Generic constraint generates a TS2344 · Issue #36852 · microsoft/TypeScript · GitHub
Open Graph Title: Generic constraint generates a TS2344 · Issue #36852 · microsoft/TypeScript
X Title: Generic constraint generates a TS2344 · Issue #36852 · microsoft/TypeScript
Description: TypeScript Version: 3.7.x, 3.8.1, 3.9.x (next) Search Terms: Generic constraint 2344 Code type KeyOf
Open Graph Description: TypeScript Version: 3.7.x, 3.8.1, 3.9.x (next) Search Terms: Generic constraint 2344 Code type KeyOf
X Description: TypeScript Version: 3.7.x, 3.8.1, 3.9.x (next) Search Terms: Generic constraint 2344 Code type KeyOf<T> = keyof ((T extends any ? (k: T)=>void : never) extends ((k: infer I)=>void) ? I ...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/36852
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Generic constraint generates a TS2344 ","articleBody":"**TypeScript Version:** 3.7.x, 3.8.1, 3.9.x (next)\r\n\r\n\u003c!-- Search terms you tried before logging this (so others can find this issue more easily) -->\r\n**Search Terms:** Generic constraint 2344 \r\n\r\n**Code**\r\n\r\n```ts\r\ntype KeyOf\u003cT> = keyof ((T extends any ? (k: T)=>void : never) extends ((k: infer I)=>void) ? I : never);\r\n\r\ntype Constraint\u003cT> = KeyOf\u003cT> extends never\r\n ? any\r\n // : KeyOf\u003cany> extends KeyOf\u003cT>\r\n // ? any\r\n : (\r\n 'a' extends KeyOf\u003cT>\r\n ? never\r\n : any\r\n );\r\n\r\ntype ForceConstraint\u003cT> = T extends Constraint\u003cT> ? T : never;\r\n\r\ninterface Interface\u003cT extends Constraint\u003cT>> {\r\n b: string;\r\n}\r\n\r\ntype Infer\u003cCB> = CB extends () => infer TReturn\r\n ? ForceConstraint\u003cTReturn>\r\n : never;\r\n\r\ntype InferWithConstraint\u003cCB> = ForceConstraint\u003cInfer\u003cCB>>;\r\n\r\ntype InferInterfaceFromCallback\u003cCB> = Interface\u003cInferWithConstraint\u003cCB>>; // invalid => NOT OK\r\ntype InferInterfaceFromCallback1\u003cCB> = Interface\u003cInfer\u003cCB>;> // invalid too => NOT OK\r\n\r\n// just debug purpose\r\ntype A1 = Interface\u003cunknown>;\r\ntype A2 = Interface\u003cany>; // invalid => OK\r\ntype A3 = Interface\u003cnever>;\r\ntype A4 = Interface\u003cundefined>;\r\ntype A5 = Interface\u003cnumber>;\r\ntype A6 = Interface\u003cstring>;\r\ntype A7 = Interface\u003cnull>;\r\ntype A8 = Interface\u003cobject>;\r\ntype A9 = Interface\u003c{}>;\r\ntype A_FAIL1 = Interface\u003c{ a: 'b' }>; // invalid => OK\r\ntype A_FAIL2 = Interface\u003c{ a: 'b' } | 1>; // invalid => OK\r\n```\r\n\r\n[Playground Link](http://www.typescriptlang.org/play/?ts=3.9.0-dev.20200215&ssl=38&ssc=54&pln=37&pc=1#)\r\n\r\n**Expected behavior:**\r\n```ts\r\ntype InferInterfaceFromCallback\u003cCB> = Interface\u003cInferWithConstraint\u003cCB>>;\r\n``` \r\nShould be valid\r\n\r\n**Explanation:**\r\n\r\nSo, i was trying to apply a specific constraint on a generic type. \r\n\r\n`Constraint\u003cT>` 'returns' never if T contains a property called ```a```, else it returns any\r\n\r\n`ForceConstraint\u003cT>` tries to help typescript to know if T is valid, else it returns never\r\n\r\n` Interface\u003cT extends Constraint\u003cT>>` forces its generic type to respect `Constraint`\r\n\r\n`Infer\u003cCB>` infers the return type of **CB** and ensures it follows `Constraint`\r\n\r\n`InferWithConstraint\u003cCB>` forces again `Infer\u003cCB>` to be compliant with `Constraint` (even if it is already)\r\n\r\nSo `Interface\u003cInferWithConstraint\u003cCB>>` should be valid, sadly, typescript reports:\r\n```\r\nTS2344: Type 'ForceConstraint\u003cInfer\u003cCB>>' does not satisfy the constraint 'Constraint\u003cForceConstraint\u003cInfer\u003cCB>>>'.\r\n```\r\n\r\n**Potentially related issues:**\r\n\r\n- https://github.com/microsoft/TypeScript/issues/31736\r\n- https://github.com/microsoft/TypeScript/issues/34604\r\n \r\n","author":{"url":"https://github.com/lifaon74","@type":"Person","name":"lifaon74"},"datePublished":"2020-02-18T13:24:55Z","url":"https://github.com/microsoft/TypeScript/issues/36852"}
| 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:70fdc2c6-a8a9-4c8c-9e90-47919a90170f |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | C2D0:157C65:26AB52A:339ABAB:6B01AD3C |
| html-safe-nonce | 0e6455adf83512af365de7b1e38e1896bea601f6aa72984bd1ccf352376ee212 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDMkQwOjE1N0M2NToyNkFCNTJBOjMzOUFCQUI6NkIwMUFEM0MiLCJ2aXNpdG9yX2lkIjoiMzE3NDA5MTk1NzIyODEyMTQwNCIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 4d27e8b0bd86e728233e4bf35223fb110cee9167bfe726ba6806f663ebde7183 |
| hovercard-subject-tag | issue:566890855 |
| 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/36852/issue_layout |
| twitter:image | https://opengraph.githubassets.com/f812b23823613b07c9c171a63ae7619ef6313fb0c8f9271cf4169081711a70ac/microsoft/TypeScript/issues/36852 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/f812b23823613b07c9c171a63ae7619ef6313fb0c8f9271cf4169081711a70ac/microsoft/TypeScript/issues/36852 |
| og:image:alt | TypeScript Version: 3.7.x, 3.8.1, 3.9.x (next) Search Terms: Generic constraint 2344 Code type KeyOf |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | lifaon74 |
| 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