Title: Typeguard should use conditional types · Issue #23764 · microsoft/TypeScript · GitHub
Open Graph Title: Typeguard should use conditional types · Issue #23764 · microsoft/TypeScript
X Title: Typeguard should use conditional types · Issue #23764 · microsoft/TypeScript
Description: TypeScript Version: 2.9.0-dev.20180428 Search Terms: conditional types, type guards Code type Pred
Open Graph Description: TypeScript Version: 2.9.0-dev.20180428 Search Terms: conditional types, type guards Code type Pred
X Description: TypeScript Version: 2.9.0-dev.20180428 Search Terms: conditional types, type guards Code type Pred<T, U extends T> = (v: T) => v is U; function someFn<T, U extends T>(pred: Pred<T...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/23764
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Typeguard should use conditional types","articleBody":"\u003c!-- 🚨 STOP 🚨 𝗦𝗧𝗢𝗣 🚨 𝑺𝑻𝑶𝑷 🚨 -->\r\n\u003c!--\r\nHalf of all issues filed here are duplicates, answered in the FAQ, or not appropriate for the bug tracker.\r\n\r\nPlease help us by doing the following steps before logging an issue:\r\n * Search: https://github.com/Microsoft/TypeScript/search?type=Issues\r\n * Read the CONTRIBUTING guidelines: https://github.com/Microsoft/TypeScript/blob/master/CONTRIBUTING.md\r\n * Read the FAQ: https://github.com/Microsoft/TypeScript/wiki/FAQ\r\n-->\r\n\r\n\u003c!-- If you have a QUESTION:\r\n THIS IS NOT A FORUM FOR QUESTIONS.\r\n Ask questions at http://stackoverflow.com/questions/tagged/typescript\r\n or https://gitter.im/Microsoft/TypeScript\r\n-->\r\n\r\n\u003c!-- If you have a SUGGESTION:\r\n Most suggestion reports are duplicates, please search extra hard before logging a new suggestion.\r\n See https://github.com/Microsoft/TypeScript-wiki/blob/master/Writing-Good-Design-Proposals.md\r\n-->\r\n\r\n\u003c!-- If you have a BUG:\r\n Please fill in the *entire* template below.\r\n-->\r\n\r\n\u003c!-- Please try to reproduce the issue with `typescript@next`. It may have already been fixed. -->\r\n**TypeScript Version:** 2.9.0-dev.20180428\r\n\r\n\u003c!-- Search terms you tried before logging this (so others can find this issue more easily) -->\r\n**Search Terms:** \r\n\r\nconditional types, type guards\r\n\r\n**Code**\r\n\r\n```ts\r\ntype Pred\u003cT, U extends T> = (v: T) => v is U;\r\nfunction someFn\u003cT, U extends T>(pred: Pred\u003cT, U>, val: T): void {\r\n if (pred(val)) {\r\n const u: U = val;\r\n console.log(u);\r\n return;\r\n }\r\n const notU: Exclude\u003cT, U> = val; // Type 'T' is not assignable to type 'Exclude\u003cT, U>'.\r\n console.log(notU);\r\n}\r\n```\r\n\r\n**Expected behavior:**\r\n\r\nThe code should compile, the predicate type-guard sets `val` to `U` in the \"true\" branch, and to `Exclude\u003cT, U>` in the \"false\" branch.\r\n\r\n**Actual behavior:**\r\n\r\nIn the \"false\" branch `val` is inferred to be `T`:\r\n\r\n```\r\nType 'T' is not assignable to type 'Exclude\u003cT, U>'.\r\n```\r\n\r\n**Playground Link:** https://bit.ly/2HAtMpF\r\n\r\n**Related Issues:**\r\n","author":{"url":"https://github.com/mkulke","@type":"Person","name":"mkulke"},"datePublished":"2018-04-28T13:44:04Z","url":"https://github.com/microsoft/TypeScript/issues/23764"}
| 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:1ed3e8e0-6cc6-0064-3633-17e009004db9 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | ED50:3D97C8:B3ADA2:F1215E:6B00F358 |
| html-safe-nonce | 68789b98672bea27f894f50e6691301e049565062995a4097bd92f4c6ab016bf |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFRDUwOjNEOTdDODpCM0FEQTI6RjEyMTVFOjZCMDBGMzU4IiwidmlzaXRvcl9pZCI6IjIwNTgzMjkyODU2MDA1Mzk0ODAiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | f8bb67adf1c8e810a20a61b26e360547c37ffdad302719d43ce9a2465a2702cf |
| hovercard-subject-tag | issue:318637926 |
| 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/23764/issue_layout |
| twitter:image | https://opengraph.githubassets.com/4dd35a3215d7d88ebe46817c3786c42acc4f2a2097b36bdd8b6354e0feee17fe/microsoft/TypeScript/issues/23764 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/4dd35a3215d7d88ebe46817c3786c42acc4f2a2097b36bdd8b6354e0feee17fe/microsoft/TypeScript/issues/23764 |
| og:image:alt | TypeScript Version: 2.9.0-dev.20180428 Search Terms: conditional types, type guards Code type Pred |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | mkulke |
| hostname | github.com |
| expected-hostname | github.com |
| None | 4ef498cbabcbe3c50befc7a5b743e7072ee770b4daf0e15e54801180d1fa91fc |
| 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 | 9983dd5da06943f833c971dd5b6bc988e2858be5 |
| ui-target | canary-2 |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width