Title: Constrain inferred types in return type positions of type guards · Issue #32894 · microsoft/TypeScript · GitHub
Open Graph Title: Constrain inferred types in return type positions of type guards · Issue #32894 · microsoft/TypeScript
X Title: Constrain inferred types in return type positions of type guards · Issue #32894 · microsoft/TypeScript
Description: Search Terms infer type guard generic conditional Suggestion When using the infer keyword in the predicate position of a type guard, the inferred type should be constrained by the type of the variable upon which it is predicated. Use Cas...
Open Graph Description: Search Terms infer type guard generic conditional Suggestion When using the infer keyword in the predicate position of a type guard, the inferred type should be constrained by the type of the varia...
X Description: Search Terms infer type guard generic conditional Suggestion When using the infer keyword in the predicate position of a type guard, the inferred type should be constrained by the type of the varia...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/32894
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Constrain inferred types in return type positions of type guards","articleBody":"## Search Terms\r\n\r\ninfer type guard generic conditional\r\n\r\n## Suggestion\r\n\r\nWhen using the `infer` keyword in the predicate position of a type guard, the inferred type should be constrained by the type of the variable upon which it is predicated.\r\n\r\n## Use Cases\r\n\r\nRecently ran into a case where we extended a simple type and wanted to extend an object of corresponding type guards to allow passing in this new type:\r\n\r\n```ts\r\ntype MyType = \"A\" | \"B\" | \"C\";\r\ntype MyExtendedType = MyType | \"D\";\r\n\r\ntype ExtendGuards\u003cT> = {\r\n [K in keyof T]:\r\n T[K] extends (value: MyType) => value is infer U\r\n ? (value: MyExtendedType) => value is U\r\n : T[K]\r\n};\r\n```\r\n[See in playground](http://www.typescriptlang.org/play/#code/C4TwDgpgBAsiAq5oF4oCICCaoB90CFs80BhNAbgChRJYQBRAD2AgDsATCdxW1OH6MQAiFStSRQmLDgHEArgEMATuwDOAHngA+KKgDelKEagBtANJQAlqygBrCCAD2AMyjwAugC5Dx3-HPuUBDMbGpQABQAbgoANnIQnnQCAJS6OtFx0JaqVqzOEEpQAKo+vmVQAPwRGfGJcFKhXClpUDVZOSXl5Yn+Zu6UAL5UQA)\r\n\r\nCurrently this produces an error both on `infer U` and on its usage on the following line:\r\n\r\n```\r\nA type predicate's type must be assignable to its parameter's type.\r\n Type 'U' is not assignable to type 'MyType'.\r\n Type 'U' is not assignable to type '\"C\"'.\r\n```\r\n\r\nIt seems that it should only be possible to extend this type guard if `U` extends `MyType`, so it should probably just be constrained by that by default.\r\n\r\n\r\n## Checklist\r\n\r\nMy suggestion meets these guidelines:\r\n\r\n* [x] This wouldn't be a breaking change in existing TypeScript/JavaScript code\r\n* [x] This wouldn't change the runtime behavior of existing JavaScript code\r\n* [x] This could be implemented without emitting different JS based on the types of the expressions\r\n* [x] This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)\r\n* [x] This feature would agree with the rest of [TypeScript's Design Goals](https://github.com/Microsoft/TypeScript/wiki/TypeScript-Design-Goals).\r\n\r\n","author":{"url":"https://github.com/jscheiny","@type":"Person","name":"jscheiny"},"datePublished":"2019-08-14T19:06:23Z","url":"https://github.com/microsoft/TypeScript/issues/32894"}
| 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:efe1e767-923d-376d-40e2-ec1306dd6387 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 92A2:E9D16:2489A27:2E5B5E7:6B01BE85 |
| html-safe-nonce | 52ec67d59bbbe0834facbab9b2313a63605ddbe75e4622bc1dea105b46e67485 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5MkEyOkU5RDE2OjI0ODlBMjc6MkU1QjVFNzo2QjAxQkU4NSIsInZpc2l0b3JfaWQiOiI4ODA0NjM1ODI1NjI0MTcwMTE3IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 8ce844e8886ab44fffdd1357bf6a0b106afe983a6773619a1eb5b7b82f4544c1 |
| hovercard-subject-tag | issue:480840620 |
| 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/32894/issue_layout |
| twitter:image | https://opengraph.githubassets.com/a2b61b9a2cb0ddba7c77339c400e3fd786862b3f8deb29b68a90f7ec6575e223/microsoft/TypeScript/issues/32894 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/a2b61b9a2cb0ddba7c77339c400e3fd786862b3f8deb29b68a90f7ec6575e223/microsoft/TypeScript/issues/32894 |
| og:image:alt | Search Terms infer type guard generic conditional Suggestion When using the infer keyword in the predicate position of a type guard, the inferred type should be constrained by the type of the varia... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | jscheiny |
| 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