Title: Logical and loses narrowed types when testing for truthyness on union types · Issue #29106 · microsoft/TypeScript · GitHub
Open Graph Title: Logical and loses narrowed types when testing for truthyness on union types · Issue #29106 · microsoft/TypeScript
X Title: Logical and loses narrowed types when testing for truthyness on union types · Issue #29106 · microsoft/TypeScript
Description: TypeScript Version: 3.3.0-dev.20181220 Search Terms: narrow union regression Code const f = (_a: string, _b: string): void => {}; interface A { a?: string; b?: string; } interface B { a: string; b: string; } type U = A | B; const u: U = ...
Open Graph Description: TypeScript Version: 3.3.0-dev.20181220 Search Terms: narrow union regression Code const f = (_a: string, _b: string): void => {}; interface A { a?: string; b?: string; } interface B { a: string; b:...
X Description: TypeScript Version: 3.3.0-dev.20181220 Search Terms: narrow union regression Code const f = (_a: string, _b: string): void => {}; interface A { a?: string; b?: string; } interface B { a: string;...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/29106
X: @github
Domain: github.com
IPv4 address: 140.82.114.3
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Logical and loses narrowed types when testing for truthyness on union types","articleBody":"\u003c!-- 🚨 STOP 🚨 𝗦𝗧𝗢𝗣 🚨 𝑺𝑻𝑶𝑷 🚨\r\n\r\nHalf of all issues filed here are duplicates, answered in the FAQ, or not appropriate for the bug tracker. Even if you think you've found a *bug*, please read the FAQ first, especially the Common \"Bugs\" That Aren't Bugs section!\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 FAQ: https://github.com/Microsoft/TypeScript/wiki/FAQ\r\n\r\nPlease 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:** 3.3.0-dev.20181220\r\n\r\n\u003c!-- Search terms you tried before logging this (so others can find this issue more easily) -->\r\n**Search Terms:** narrow union regression\r\n\r\n**Code**\r\n\r\n```ts\r\nconst f = (_a: string, _b: string): void => {};\r\n\r\ninterface A {\r\n a?: string;\r\n b?: string;\r\n}\r\n\r\ninterface B {\r\n a: string;\r\n b: string;\r\n}\r\n\r\ntype U = A | B;\r\n\r\nconst u: U = {} as any;\r\n\r\n// u.b narrowed, but not a\r\n// Argument of type 'string | undefined' is not assignable to parameter of type 'string'.\r\n// Type 'undefined' is not assignable to type 'string'.\r\nu.a && u.b && f(u.a, u.b);\r\n\r\n// Error: u.a narrowed, but not b\r\n// Argument of type 'string | undefined' is not assignable to parameter of type 'string'.\r\n// Type 'undefined' is not assignable to type 'string'.\r\nu.b && u.a && f(u.a, u.b);\r\n\r\n// Works\r\ntypeof u.a === \"string\" && typeof u.b === \"string\" && f(u.a, u.b);\r\n\r\n// Works\r\nconst a: A = {} as any;\r\na.b && a.a && f(a.a, a.b);\r\n```\r\n\r\n**Expected behavior:**\r\n\r\nAll forms narrow correctly and compile with strictNullChecks.\r\n\r\nThis works correctly in 3.1.6\r\n\r\n**Actual behavior:**\r\n\r\nOnly non-union or explicitly checking typeof narrows correctly in >=3.2.\r\n\r\n**Playground Link:** (Needs strictNullChecks enabled) http://www.typescriptlang.org/play/#src=const%20f%20%3D%20(_a%3A%20string%2C%20_b%3A%20string)%3A%20void%20%3D%3E%20%7B%7D%3B%0D%0A%0D%0Ainterface%20A%20%7B%0D%0A%20%20a%3F%3A%20string%3B%0D%0A%20%20b%3F%3A%20string%3B%0D%0A%7D%0D%0A%0D%0Ainterface%20B%20%7B%0D%0A%20%20a%3A%20string%3B%0D%0A%20%20b%3A%20string%3B%0D%0A%7D%0D%0A%0D%0Atype%20U%20%3D%20A%20%7C%20B%3B%0D%0A%0D%0Aconst%20u%3A%20U%20%3D%20%7B%7D%20as%20any%3B%0D%0A%0D%0A%2F%2F%20u.b%20narrowed%2C%20but%20not%20a%0D%0A%2F%2F%20Argument%20of%20type%20'string%20%7C%20undefined'%20is%20not%20assignable%20to%20parameter%20of%20type%20'string'.%0D%0A%2F%2F%20%20%20Type%20'undefined'%20is%20not%20assignable%20to%20type%20'string'.%0D%0Au.a%20%26%26%20u.b%20%26%26%20f(u.a%2C%20u.b)%3B%0D%0A%0D%0A%2F%2F%20Error%3A%20u.a%20narrowed%2C%20but%20not%20b%0D%0A%2F%2F%20Argument%20of%20type%20'string%20%7C%20undefined'%20is%20not%20assignable%20to%20parameter%20of%20type%20'string'.%0D%0A%2F%2F%20%20%20Type%20'undefined'%20is%20not%20assignable%20to%20type%20'string'.%0D%0Au.b%20%26%26%20u.a%20%26%26%20f(u.a%2C%20u.b)%3B%0D%0A%0D%0A%2F%2F%20Works%0D%0Atypeof%20u.a%20%3D%3D%3D%20%22string%22%20%26%26%20typeof%20u.b%20%3D%3D%3D%20%22string%22%20%26%26%20f(u.a%2C%20u.b)%3B%0D%0A%0D%0A%2F%2F%20Works%0D%0Aconst%20a%3A%20A%20%3D%20%7B%7D%20as%20any%3B%0D%0Aa.b%20%26%26%20a.a%20%26%26%20f(a.a%2C%20a.b)%3B%0D%0A\r\n\r\n**Related Issues:**\r\n\r\n#29012 \r\n\r\n","author":{"url":"https://github.com/asmundg","@type":"Person","name":"asmundg"},"datePublished":"2018-12-20T13:41:18Z","url":"https://github.com/microsoft/TypeScript/issues/29106"}
| 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:f22c8640-695b-c34f-eebf-5658f510cf53 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 9A02:81334:5A2387:7D6026:6B0481C5 |
| html-safe-nonce | 60123d34271aef35471170de9e1f5927147812327df4e57faa64369f2e78a128 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5QTAyOjgxMzM0OjVBMjM4Nzo3RDYwMjY6NkIwNDgxQzUiLCJ2aXNpdG9yX2lkIjoiNTcyMDE4Nzk0ODIxMzkwNzciLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | fa91d61ee6857d2a42aa0a17a4ae4787d22e65c6dfa9c5e2b0cc2199af027e66 |
| hovercard-subject-tag | issue:393051495 |
| 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/29106/issue_layout |
| twitter:image | https://opengraph.githubassets.com/39151ca4fb3443a979caa148a44e232a58f1ef12e703f792590a6bfeba922960/microsoft/TypeScript/issues/29106 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/39151ca4fb3443a979caa148a44e232a58f1ef12e703f792590a6bfeba922960/microsoft/TypeScript/issues/29106 |
| og:image:alt | TypeScript Version: 3.3.0-dev.20181220 Search Terms: narrow union regression Code const f = (_a: string, _b: string): void => {}; interface A { a?: string; b?: string; } interface B { a: string; b:... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | asmundg |
| hostname | github.com |
| expected-hostname | github.com |
| None | 0366ddfb372682a32e92a5d86155cb08fa83e5bbe79953a968789f2f7e24c511 |
| 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 | ed3f2e01f6b7672780c586c8af0e72726374d7b6 |
| 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