Title: Predicate inference fails for empty object and nullish values · Issue #58967 · microsoft/TypeScript · GitHub
Open Graph Title: Predicate inference fails for empty object and nullish values · Issue #58967 · microsoft/TypeScript
X Title: Predicate inference fails for empty object and nullish values · Issue #58967 · microsoft/TypeScript
Description: 🔎 Search Terms "infer predicate object types", "infer predicate empty object", "infer predicate empty object null", "{} | null" 🕗 Version & Regression Information This is a type narrowing issue This is the behavior in every version I tri...
Open Graph Description: 🔎 Search Terms "infer predicate object types", "infer predicate empty object", "infer predicate empty object null", "{} | null" 🕗 Version & Regression Information This is a type narrowing issue Thi...
X Description: 🔎 Search Terms "infer predicate object types", "infer predicate empty object", "infer predicate empty object null", "{} | null" 🕗 Version & Regression In...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/58967
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Predicate inference fails for empty object and nullish values","articleBody":"### 🔎 Search Terms\r\n\r\n\"infer predicate object types\", \"infer predicate empty object\", \"infer predicate empty object null\", \"{} | null\"\r\n\r\n### 🕗 Version & Regression Information\r\n\r\n- This is a type narrowing issue\r\n- This is the behavior in every version I tried\r\n\r\n### ⏯ Playground Link\r\n\r\nhttps://www.typescriptlang.org/play/?ts=5.5.2#code/MYewdgzgLgBApgWwA5QJ4DkCuAbbMC8MA2gN4C+ANDGDtgLoB0AZgJbZRwBOAFBAQHwwAhEIgBKANwwA9NPgAPJHGAcAJjHJE6AKBh7ZMAHoB+baEixEKVAFUwquKzBx1hUpRiZ7jls9WNWdi5eAWFRSRk5OEVlNQ0yLV19ORMzcGgFJGwWYBYoLFwWCAALAmJyKhpcALYOHj58QT4hfEIq7AiDEABrJMijU3MMqzQsBAAjLjL3KgAGGqD60JFxKS7evX7UoctkNAAVTkwoYtRp8gW6kMaw1f7opRUXeMTNg2303esAMQBDbAgZzcF2YtWCDUE4TWURiT3UzgAblxXskBmkLNRwABRPYYWjnGBMEAgABc8RgHnal3Byyh-R6fXeg0+1FoRVKbnaVC8DicLmpSxuK06MMecURyJ0bxSpiAA\r\n\r\n### 💻 Code\r\n\r\n```ts\r\nconst emptyNull = [{}, null].filter(s => !!s); // expected {}[]\r\n // ^? ({} | null)[]\r\nconst emptyUndefined = [{}, undefined].filter(s => !!s); // expected {}[]\r\n // ^? ({} | undefined)\r\nconst emptyTruthy = [{}].filter(s => !!s); // expected {}[]\r\n // ^? {}[]\r\nconst emptyFalsy = [{}].filter(s => !s); // expected never[]\r\n // ^? {}[]\r\nconst nullish = [null, undefined].filter(s => !!s); // expected never[]\r\n // ^? (null | undefined)[]\r\nconst explicitNullish = [{}, null].filter(s => s !== null); // ok\r\n // ^? { foo: {}; }[]\r\nconst nonEmptyNull = [{ foo: {} }, null].filter(s => !!s); // ok\r\n // ^? { foo: {}; }[]\r\nconst emptyNumber = [{}, 0].filter(s => !!s); // ok\r\n // ^? {}[]\r\n```\r\n\r\n### 🙁 Actual behavior\r\n\r\nPredicate inference fails for the cases where `{}` is declared along with a nullish value, using a predicate to test for truthiness or falsiness (`!!x` / `!x`). It infers a type that includes the nullishness.\r\n\r\nIt also fails to infer the predicate for cases where array only contains nullish values.\r\n\r\nIt works if the object has at least one property, or using explicit bullishness check.\r\n\r\n### 🙂 Expected behavior\r\n\r\nThe return type should reflect the truthiness of `{}`, as it is neither a nullish or falsy value and should pass the truthiness test.\r\n\r\n### Additional information about the issue\r\n\r\nThe section \"This is a gentle nudge away from truthiness footguns\" in https://github.com/microsoft/TypeScript/pull/57465 explains that object types should have no footguns:\r\n\r\n> If you're working with object types, on the other hand, there is no footgun\r\n> and a truthiness test will infer a predicate:\r\n> ```ts\r\n> const datesTruthy = [new Date(), null, new Date(), null].filter(d => !!d);\r\n> // ^? const datesTruthy: Date[]\r\n> ```\r\n\r\nThat's why I expected the same for `{}` and the opposite with nullish-only values.","author":{"url":"https://github.com/fnky","@type":"Person","name":"fnky"},"datePublished":"2024-06-21T21:21:36Z","url":"https://github.com/microsoft/TypeScript/issues/58967"}
| 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:d045f98f-f173-9a64-b2a9-6f7371e9750f |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 895E:E3C4F:9B881E:C7981E:6B00F228 |
| html-safe-nonce | 54fe83d0a97ad3d113aa6ca7060f865ff9cfad97b0b415eb67a9e3c1a644a371 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4OTVFOkUzQzRGOjlCODgxRTpDNzk4MUU6NkIwMEYyMjgiLCJ2aXNpdG9yX2lkIjoiNzA0Nzk2NjAzMDg2OTYyMzMzNiIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | dfc5d9c11321c538e748baf89bd47d7ad3d5a44008d7a7847961005931536299 |
| hovercard-subject-tag | issue:2367265353 |
| 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/58967/issue_layout |
| twitter:image | https://opengraph.githubassets.com/40574a99cbbfc2f0724cfc6b2a50fc569294a695f690c2b142cd996b6d7cbc69/microsoft/TypeScript/issues/58967 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/40574a99cbbfc2f0724cfc6b2a50fc569294a695f690c2b142cd996b6d7cbc69/microsoft/TypeScript/issues/58967 |
| og:image:alt | 🔎 Search Terms "infer predicate object types", "infer predicate empty object", "infer predicate empty object null", "{} | null" 🕗 Version & Regression Information This is a type narrowing issue Thi... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | fnky |
| 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 | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width