Title: Type narrowing for predicates works with implicit if, fails with explicit if · Issue #53005 · microsoft/TypeScript · GitHub
Open Graph Title: Type narrowing for predicates works with implicit if, fails with explicit if · Issue #53005 · microsoft/TypeScript
X Title: Type narrowing for predicates works with implicit if, fails with explicit if · Issue #53005 · microsoft/TypeScript
Description: Bug Report See https://tsplay.dev/Nr8YVW - it seems like the type narrowing allows truthy/falsy checks, but doesn't support when the predicate's return value is explicitly compared to true or false. 🔎 Search Terms i guess type narrowing,...
Open Graph Description: Bug Report See https://tsplay.dev/Nr8YVW - it seems like the type narrowing allows truthy/falsy checks, but doesn't support when the predicate's return value is explicitly compared to true or false...
X Description: Bug Report See https://tsplay.dev/Nr8YVW - it seems like the type narrowing allows truthy/falsy checks, but doesn't support when the predicate's return value is explicitly compared to true ...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/53005
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Type narrowing for predicates works with implicit if, fails with explicit if","articleBody":"# Bug Report\r\n\r\nSee https://tsplay.dev/Nr8YVW - it seems like the type narrowing allows truthy/falsy checks, but doesn't support when the predicate's return value is explicitly compared to `true` or `false`.\r\n\r\n### 🔎 Search Terms\r\n\r\ni guess type narrowing, predicates?\r\n\r\n### 🕗 Version & Regression Information\r\n\r\nThis occurs in the latest nightly on the TS playground; see above. I assume it's always been the case since predicate support was added, but haven't checked.\r\n\r\n### ⏯ Playground Link\r\n\r\nhttps://tsplay.dev/Nr8YVW\r\n\r\n### 💻 Code\r\n\r\n```ts\r\nfunction isFunction(x: unknown): x is Function {\r\n\treturn typeof x === 'function';\r\n}\r\n\r\nconst x: unknown = function () {};\r\n\r\nif (isFunction(x)) {\r\n\tx;\r\n\t// x is a function here\r\n}\r\n\r\nif (!isFunction(x)) {\r\n\tx;\r\n\t// x is not a function here (should be more specific than `unknown`, but ¯\\_(ツ)_/¯)\r\n}\r\n\r\nif (isFunction(x) !== false) {\r\n\tx;\r\n\t// x should be a function here\r\n}\r\n\r\nif (isFunction(x) === true) {\r\n\tx;\r\n\t// x should be a function here\r\n}\r\n```\r\n\r\n### 🙁 Actual behavior\r\n\r\nSee above.\r\n\r\n### 🙂 Expected behavior\r\n\r\nSee above.","author":{"url":"https://github.com/ljharb","@type":"Person","name":"ljharb"},"datePublished":"2023-02-27T20:02:27Z","url":"https://github.com/microsoft/TypeScript/issues/53005"}
| 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:396a9235-a5b9-9754-2408-8ec522478c8e |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | B7B0:B1B84:18D3EFA:1F83353:6B0180E4 |
| html-safe-nonce | 6b86bb910765000ce816589f729b491fd9f12b67d2baf14dcc7f852aa7a27635 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCN0IwOkIxQjg0OjE4RDNFRkE6MUY4MzM1Mzo2QjAxODBFNCIsInZpc2l0b3JfaWQiOiI3ODc5NDUzMTc0MTU4Nzg4ODQiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | a54f7974c78b7169385711896cb2d323002d67721ff4758770d35ae65dfb40d1 |
| hovercard-subject-tag | issue:1601858270 |
| 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/53005/issue_layout |
| twitter:image | https://opengraph.githubassets.com/33807a2b204c44c304d379b2fa7d4aae58b354f8df968832a3e4a0d6c32dc04c/microsoft/TypeScript/issues/53005 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/33807a2b204c44c304d379b2fa7d4aae58b354f8df968832a3e4a0d6c32dc04c/microsoft/TypeScript/issues/53005 |
| og:image:alt | Bug Report See https://tsplay.dev/Nr8YVW - it seems like the type narrowing allows truthy/falsy checks, but doesn't support when the predicate's return value is explicitly compared to true or false... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | ljharb |
| 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