Title: Type guard does not work correctly with empty string type · Issue #42101 · microsoft/TypeScript · GitHub
Open Graph Title: Type guard does not work correctly with empty string type · Issue #42101 · microsoft/TypeScript
X Title: Type guard does not work correctly with empty string type · Issue #42101 · microsoft/TypeScript
Description: Bug Report 🔎 Search Terms type guard 🕗 Version & Regression Information TypeScript 3.3.3+ (as per playground) Please keep and fill in the line that best applies: This is the behavior in every version I tried, and I reviewed the FAQ for e...
Open Graph Description: Bug Report 🔎 Search Terms type guard 🕗 Version & Regression Information TypeScript 3.3.3+ (as per playground) Please keep and fill in the line that best applies: This is the behavior in every versi...
X Description: Bug Report 🔎 Search Terms type guard 🕗 Version & Regression Information TypeScript 3.3.3+ (as per playground) Please keep and fill in the line that best applies: This is the behavior in every v...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/42101
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Type guard does not work correctly with empty string type","articleBody":"# Bug Report\r\n\r\n### 🔎 Search Terms\r\ntype guard\r\n\r\n### 🕗 Version & Regression Information\r\nTypeScript 3.3.3+ (as per playground)\r\n\r\nPlease keep and fill in the line that best applies:\r\n- This is the behavior in every version I tried, and I reviewed the FAQ for entries about [Common \"Bugs\" That Aren't Bugs](https://github.com/Microsoft/TypeScript/wiki/FAQ#common-bugs-that-arent-bugs)\r\n\r\n### ⏯ Playground Link\r\n\r\n[Playground link with error](https://www.typescriptlang.org/play?ts=4.1.3#code/C4TwDgpgBAogtmUBlYAnAlgOwOZQLxQDkhUAPlJgK4A21ZUlmAJhAGZYRMDcAsAFD9WjAMbB0Ae0xR0AZ3iIQACgBuAQ2qUIALigy0WXOXnJ9OAJQ61G6LNgITGHFADe-KO6ioIwSqilXNfDwCYjJyAOhggipaMKgIoIJGFnZMTl4+AF9+fmpvKGAIPR09R0MKGjpyZLYObn5hST1PVWZxOHwoAFlVYAALADpWanFxVEUe-oHUVqZ2xTMoACpu3sHh0fGAZjMzDPRWKEUZto6oqAAGRdc+DwKi4E7iDMyoCGoZG0Pj2fbEqAAjNc3B5Cs0ksxaml6lk3h9oDc7mDHtFKi8cnwDkdZMYlMjdi4Qe5GpgZOI8gMRthFAADCI6AAkzmRmRpeyJ0m+yP+xGBtzuHhJZIpVMUhAAcuJIIR2fz3NksvwgA)\r\n\r\n[Playground link without error but with incorrect type](https://www.typescriptlang.org/play?ts=4.1.3#code/C4TwDgpgBAogtmUBlYAnAlgOwOZQLxQDkhUAPlAK6YAmEAZlhNQNwCwAUB3VQMbDoB7TFHQBneIhAAKAG4BDADYUIALiii0WXOQnJNOAJRr5S6GNgI9GHFADeHKI6ioIwCqmEnl+PAWJlyL2hfAipaBkwmNnYAXw4OBVcoYAgNNQ1rbUoaekYWDh4hDWc5GgE4fCgAWTlgAAsAOjoFAQFUKRr6htRS6nKpAygAKmraxubW9oBmAwNo9DooKR6yipCoAAZB+3YnZNTgSuJomKgIBVEzReXe8p8CAEZthycU4tCciKiOOM52BaWYl00jewFmdhejkKmFEAkSDRa2CkAAMgmoACS2UExZFzSEia6g+5EQjPXZ7JzQ2HwxFSQgAOQEkFJ0T2v1+QA)\r\n\r\n\r\n### 💻 Code\r\n\r\n**The compiled JavaScript version works correctly, but TypeScript gives errors**:\r\n```ts\r\ntype EmptyString = '' | null | undefined;\r\n\r\nfunction isEmpty(value: string | EmptyString): value is EmptyString {\r\n return value === '' || value === null || value === undefined;\r\n}\r\n\r\nlet test: string | null | undefined;\r\n\r\nif (isEmpty(test)) {\r\n if (test === '') { // Compilation error since test: null | undefined and not test: '' | null | undefined\r\n console.log('Nope');\r\n }\r\n}\r\n\r\n```\r\n\r\n\r\n\r\n**Also, it's weird that when I remove null from the list of types, the error disappears, but the incorrect type is still shown**:\r\n```ts\r\ntype EmptyString = '' | undefined;\r\n\r\nfunction isEmpty(value: string | EmptyString): value is EmptyString {\r\n return value === '' || value === undefined;\r\n}\r\n\r\nlet test: string | undefined;\r\n\r\nif (isEmpty(test)) {\r\n if (test === '') { // No errors, but the editor displays incorrect type.\r\n console.log('Nope');\r\n }\r\n}\r\n\r\n```\r\n\r\n\r\n\r\n### 🙁 Actual behavior\r\nTypeGuard ignores empty string and thinks the value can only be null or undefined.\r\n\r\n### 🙂 Expected behavior\r\nThe type should include empty string. No errors expected.","author":{"url":"https://github.com/olderor","@type":"Person","name":"olderor"},"datePublished":"2020-12-24T09:34:09Z","url":"https://github.com/microsoft/TypeScript/issues/42101"}
| 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:5669932d-7dbd-8e9f-4b13-f0186ee25532 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | C51A:2564F:C9A74D:10B5785:6B00F53D |
| html-safe-nonce | 71b536edde90a66bafbcf5519882da0ae43a0ac56ad2701710ce51ea24134217 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDNTFBOjI1NjRGOkM5QTc0RDoxMEI1Nzg1OjZCMDBGNTNEIiwidmlzaXRvcl9pZCI6IjI2Njc5NjcwMjM4ODMwMjM2NzciLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | a7804ce2c2941dcd80ed519f1986fe15c70cb15ecb672b3e7382bda706064548 |
| hovercard-subject-tag | issue:774295165 |
| 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/42101/issue_layout |
| twitter:image | https://opengraph.githubassets.com/f079b8751786ec3012d2b1a6a09c49274c0f7ff67c18fd0f78f6e29756fd322d/microsoft/TypeScript/issues/42101 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/f079b8751786ec3012d2b1a6a09c49274c0f7ff67c18fd0f78f6e29756fd322d/microsoft/TypeScript/issues/42101 |
| og:image:alt | Bug Report 🔎 Search Terms type guard 🕗 Version & Regression Information TypeScript 3.3.3+ (as per playground) Please keep and fill in the line that best applies: This is the behavior in every versi... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | olderor |
| 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