Title: Type narrowed to `never`, incorrectly · Issue #27103 · microsoft/TypeScript · GitHub
Open Graph Title: Type narrowed to `never`, incorrectly · Issue #27103 · microsoft/TypeScript
X Title: Type narrowed to `never`, incorrectly · Issue #27103 · microsoft/TypeScript
Description: TypeScript Version: latest Search Terms: n/a Code function f(a?: {}) { if (!a) { // filters out falsy values: // - `null` // - `undefined` // - `false` // - `NaN` // - `0` / `-0` // - `''` return {}; } // here, the type of `a` has been n...
Open Graph Description: TypeScript Version: latest Search Terms: n/a Code function f(a?: {}) { if (!a) { // filters out falsy values: // - `null` // - `undefined` // - `false` // - `NaN` // - `0` / `-0` // - `''` return {...
X Description: TypeScript Version: latest Search Terms: n/a Code function f(a?: {}) { if (!a) { // filters out falsy values: // - `null` // - `undefined` // - `false` // - `NaN` // - `0` / `-0` // - `''` ...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/27103
X: @github
Domain: github.com
IPv4 address: 140.82.113.4
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Type narrowed to `never`, incorrectly","articleBody":"**TypeScript Version:** latest\r\n\r\n**Search Terms:** n/a\r\n\r\n**Code**\r\n\r\n```ts\r\nfunction f(a?: {}) {\r\n if (!a) {\r\n // filters out falsy values:\r\n // - `null`\r\n // - `undefined`\r\n // - `false`\r\n // - `NaN`\r\n // - `0` / `-0`\r\n // - `''`\r\n return {};\r\n }\r\n\r\n // here, the type of `a` has been narrowed from \"optional {}\"\r\n // to just \"{} minus `object`\" (object-coercible, ie, anything but null/undefined, but not `object`)\r\n // effectively, this is `string | number | symbol | boolean | Function`\r\n\r\n if (typeof a !== 'object') {\r\n // `a` here could be the following values:\r\n // - a function\r\n // - `true``\r\n // - a non-zero non-`NaN`` number\r\n // - a non-empty string\r\n // - a symbol\r\n return a; // however, the type inferred here is `never`\r\n }\r\n\r\n // here, `a` has a verifiably certain type of `object`.\r\n // because of the type guard above.\r\n return a; // however, the type is reported as `{}` \r\n}\r\n```\r\n\r\n**Expected behavior:** The type of `a` inside the `if` block will be either `{}`, or better, `string | number | symbol | boolean | Function`.\r\n\r\n**Actual behavior:** The type of `a` inside the `if` block is `never`, which is flat out incorrect.\r\n\r\n**Playground Link:** [link](http://www.typescriptlang.org/play/#src=function%20f(a%3F%3A%20%7B%7D)%20%7B%0A%20%20%20%20if%20(!a)%20%7B%0A%20%20%20%20%20%20%20%20%2F%2F%20filters%20out%20falsy%20values%3A%0A%20%20%20%20%20%20%20%20%2F%2F%20%20-%20%60null%60%0A%20%20%20%20%20%20%20%20%2F%2F%20%20-%20%60undefined%60%0A%20%20%20%20%20%20%20%20%2F%2F%20%20-%20%60false%60%0A%20%20%20%20%20%20%20%20%2F%2F%20%20-%20%60NaN%60%0A%20%20%20%20%20%20%20%20%2F%2F%20%20-%20%600%60%20%2F%20%60-0%60%0A%20%20%20%20%20%20%20%20%2F%2F%20%20-%20%60''%60%0A%20%20%20%20%20%20%20%20return%20%7B%7D%3B%0A%20%20%20%20%7D%0A%0A%20%20%20%20%2F%2F%20here%2C%20the%20type%20of%20%60a%60%20has%20been%20narrowed%20from%20%22optional%20%7B%7D%22%0A%20%20%20%20%2F%2F%20to%20just%20%22%7B%7D%20minus%20%60object%60%22%20(object-coercible%2C%20ie%2C%20anything%20but%20null%2Fundefined%2C%20but%20not%20%60object%60)%0A%20%20%20%20%2F%2F%20effectively%2C%20this%20is%20%60string%20%7C%20number%20%7C%20symbol%20%7C%20boolean%20%7C%20Function%60%0A%0A%20%20%20%20if%20(typeof%20a%20!%3D%3D%20'object')%20%7B%0A%20%20%20%20%20%20%20%20%2F%2F%20%60a%60%20here%20could%20be%20the%20following%20values%3A%0A%20%20%20%20%20%20%20%20%2F%2F%20%20%20-%20a%20function%0A%20%20%20%20%20%20%20%20%2F%2F%20%20%20-%20%60true%60%60%0A%20%20%20%20%20%20%20%20%2F%2F%20%20%20-%20a%20non-zero%20non-%60NaN%60%60%20number%0A%20%20%20%20%20%20%20%20%2F%2F%20%20%20-%20a%20non-empty%20string%0A%20%20%20%20%20%20%20%20%2F%2F%20%20%20-%20a%20symbol%0A%20%20%20%20%20%20%20%20return%20a%3B%20%2F%2F%20however%2C%20the%20type%20inferred%20here%20is%20%60never%60%0A%20%20%20%20%7D%0A%0A%20%20%20%20%2F%2F%20here%2C%20%60a%60%20has%20a%20verifiably%20certain%20type%20of%20%60object%60.%0A%20%20%20%20%2F%2F%20because%20of%20the%20type%20guard%20above.%0A%20%20%20%20return%20a%3B%20%2F%2F%20however%2C%20the%20type%20is%20reported%20as%20%60%7B%7D%60%20%0A%7D)\r\n\r\n**Related Issues:** not sure how to search for it\r\n","author":{"url":"https://github.com/ljharb","@type":"Person","name":"ljharb"},"datePublished":"2018-09-14T21:41:56Z","url":"https://github.com/microsoft/TypeScript/issues/27103"}
| 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:b3a8b816-9661-f1b0-f583-9dbe30d72026 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | E238:221EB5:675BBB:8E7EAE:6B04A5DF |
| html-safe-nonce | c98f19928dba9ce2eb3ec1b52dc966ba6b065d78a2be9078fb7b9b2db259660f |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFMjM4OjIyMUVCNTo2NzVCQkI6OEU3RUFFOjZCMDRBNURGIiwidmlzaXRvcl9pZCI6IjIzNjIyNjgyNjQxMTk0NDQ5NTkiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | e33ad276b30a2b74738d64ddc903b918ccd060f364e309a76e7db533b7b82ae8 |
| hovercard-subject-tag | issue:360463691 |
| 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/27103/issue_layout |
| twitter:image | https://opengraph.githubassets.com/bb7787c4bc3917408fe09b0b2876bf2c943a78da6ad77caa9bfdc65e71134f6f/microsoft/TypeScript/issues/27103 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/bb7787c4bc3917408fe09b0b2876bf2c943a78da6ad77caa9bfdc65e71134f6f/microsoft/TypeScript/issues/27103 |
| og:image:alt | TypeScript Version: latest Search Terms: n/a Code function f(a?: {}) { if (!a) { // filters out falsy values: // - `null` // - `undefined` // - `false` // - `NaN` // - `0` / `-0` // - `''` return {... |
| 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 | 188497a0f45a7aa8f7c866faa1950d1a7f8d82b59416adf549b222ad1d1dd19b |
| 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 | 557cfbac6cdb532730c95c90f2cefdc0c4128029 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
| Server | github.com |
| Content-Type | text/html; charset=utf-8 |
| Cache-Control | max-age=0, private, must-revalidate |
Viewport: width=device-width