Title: Incorrect tuple type narrowing · Issue #23512 · microsoft/TypeScript · GitHub
Open Graph Title: Incorrect tuple type narrowing · Issue #23512 · microsoft/TypeScript
X Title: Incorrect tuple type narrowing · Issue #23512 · microsoft/TypeScript
Description: TypeScript Version: 2.8.1 Search Terms: Strict tuple, stricter tuples, tuple type narrowing, tuple union type, conditional tuple Code type x = [string] type y = [string, number] type z = x | y function f(arg: z) { if (arg[1] !== undefine...
Open Graph Description: TypeScript Version: 2.8.1 Search Terms: Strict tuple, stricter tuples, tuple type narrowing, tuple union type, conditional tuple Code type x = [string] type y = [string, number] type z = x | y func...
X Description: TypeScript Version: 2.8.1 Search Terms: Strict tuple, stricter tuples, tuple type narrowing, tuple union type, conditional tuple Code type x = [string] type y = [string, number] type z = x | y func...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/23512
X: @github
Domain: github.com
IPv4 address: 140.82.114.4
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Incorrect tuple type narrowing","articleBody":"**TypeScript Version:** 2.8.1\r\n\r\n**Search Terms:** Strict tuple, stricter tuples, tuple type narrowing, tuple union type, conditional tuple\r\n\r\n**Code**\r\n\r\n```ts\r\ntype x = [string]\r\ntype y = [string, number]\r\ntype z = x | y\r\n\r\nfunction f(arg: z) {\r\n\tif (arg[1] !== undefined) {\r\n\t\tconst result = arg // z; should be y\r\n\t\tconst first = result[1] // string | number; should be number\r\n\t\treturn first\r\n\t} else {\r\n\t\treturn undefined\r\n\t}\r\n}\r\n```\r\n\r\n**Expected behavior:**\r\n\r\nI would expect the type system to narrow the types based on unions of tuples. \r\n\r\n**Actual behavior:**\r\n\r\nDoesn't happen.\r\n\r\n**Playground Link:**\r\n\r\nhttp://www.typescriptlang.org/play/#src=type%20x%20%3D%20%5Bstring%5D%0D%0Atype%20y%20%3D%20%5Bstring%2C%20number%5D%0D%0Atype%20z%20%3D%20x%20%7C%20y%0D%0A%0D%0Afunction%20f(arg%3A%20z)%20%7B%0D%0A%09if%20(arg%5B1%5D%20!%3D%3D%20undefined)%20%7B%0D%0A%09%09const%20result%20%3D%20arg%20%20%20%20%20%20%2F%2F%20z%3B%20should%20be%20y%0D%0A%09%09const%20first%20%3D%20result%5B1%5D%20%2F%2F%20string%20%7C%20number%3B%20should%20be%20number%0D%0A%09%09return%20first%0D%0A%09%7D%20else%20%7B%0D%0A%09%09return%20undefined%0D%0A%09%7D%0D%0A%7D\r\n\r\n**Related Issues:**\r\n","author":{"url":"https://github.com/ccorcos","@type":"Person","name":"ccorcos"},"datePublished":"2018-04-18T17:41:23Z","url":"https://github.com/microsoft/TypeScript/issues/23512"}
| 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:1f81e2b1-0f3c-7b6f-3c46-702c676774ea |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | B0B8:3C07AF:178A04:20CEAE:6B0465C5 |
| html-safe-nonce | b498366dfcb1f9aef40897ecee128c12fc84fac75b860b675ab30781280c4a6d |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCMEI4OjNDMDdBRjoxNzhBMDQ6MjBDRUFFOjZCMDQ2NUM1IiwidmlzaXRvcl9pZCI6IjUzMjgxMzg3NTc2MzgyMTkyMDUiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 7e94c38326af8226d514763dec0ba3266cd80a5db8a4a89b267a0b942bc3b97e |
| hovercard-subject-tag | issue:315577023 |
| 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/23512/issue_layout |
| twitter:image | https://opengraph.githubassets.com/f67dc3996a7d4843e31896deaad9e740141ff6fb6aff26517966b12cb646de31/microsoft/TypeScript/issues/23512 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/f67dc3996a7d4843e31896deaad9e740141ff6fb6aff26517966b12cb646de31/microsoft/TypeScript/issues/23512 |
| og:image:alt | TypeScript Version: 2.8.1 Search Terms: Strict tuple, stricter tuples, tuple type narrowing, tuple union type, conditional tuple Code type x = [string] type y = [string, number] type z = x | y func... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | ccorcos |
| hostname | github.com |
| expected-hostname | github.com |
| None | 83e4c0e58c5162177f3c161c5fbad38527ae04c11ac14980f9f9d85ae2027b1f |
| 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 | ca4d07a7cef4e4b75f821dc25e7300d80c1450a9 |
| 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