Title: Array.isArray refinement loses the array's type · Issue #32497 · microsoft/TypeScript · GitHub
Open Graph Title: Array.isArray refinement loses the array's type · Issue #32497 · microsoft/TypeScript
X Title: Array.isArray refinement loses the array's type · Issue #32497 · microsoft/TypeScript
Description: I realize there are a number of related issues. However, some where about ReadonlyArray, some didn't see directly related at all even though it was pointed there. Also, this is a much smaller example, just a single line of code really: P...
Open Graph Description: I realize there are a number of related issues. However, some where about ReadonlyArray, some didn't see directly related at all even though it was pointed there. Also, this is a much smaller examp...
X Description: I realize there are a number of related issues. However, some where about ReadonlyArray, some didn't see directly related at all even though it was pointed there. Also, this is a much smaller e...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/32497
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Array.isArray refinement loses the array's type","articleBody":"I realize there are a number of related issues. However, some where about ReadonlyArray, some didn't see directly related at all even though it was pointed there.\r\n\r\nAlso, this is a much smaller example, just a single line of code really:\r\n\r\n[Playground link](http://www.typescriptlang.org/play/index.html#code/GYVwdgxgLglg9mABAEwKYFs4AoZQFyICSUqATgIYBGANqgDxgjqVkB8AlAY82QNoC6iAN4AoROMQQEAZyiJypCgE9EAXkQBBReSUA6GNK3KcUdogD8iXIgK9d93PwDcIsRID07xABFU1GCwUJFy6tGAA5lAAFlbS8oiypDARiOio0XDIADSIlCByYHCIZKRwpLmoEOQg0qiIcMCIUEoADnUARORgSu2IwGWIYG7ipOkgpEgKyrro5C1YSKqsg6GoEdHsLgC+IkA)\r\n\r\n```js\r\nfunction demo(it: Iterable\u003cnumber>): number[] {\r\n // THIS LINE: \"array\" is any[]\r\n const array = Array.isArray(it) ? it : [...it];\r\n // Deliberate: n.length is a string method, but no error because of type \"any\" for n\r\n return array.map(n => n.length);\r\n}\r\n```\r\n\r\nI know people will point to this ir that implementation or design decision detail — honestly guys, you got it REVERSED. The tool should serve the purpose, not the other way around!\r\n\r\nThat the array loses all it's type information in such a simple example is A BUG.\r\n\r\nAnd **it is possible** to do much better: [See here!](https://flow.org/try/#0GYVwdgxgLglg9mABAEwKYFs4AoZQFyICSUqATgIYBGANqgDxgjqVkB8AlAY82QNoC6iAN4AoROMQQEAZyiJypCgE9EAXkQBBReSUA6GNK3KcUdogD8iXIgK9d93PwDcYiQHo3iACKpqMFhQkXLq0YADmUAAWVtLyiLKkMOGI6KhRcMgANIiUIHJgcIhkpHCkOagQ5CDSqIhwwIhQSgAOtQBE5GBKbYjApYhgruKkaSCkSArKuujkzVhIqqwDIajhUewuAL4iQA) I'm only pointing this out because at times the responses sound like \"We tried everything but it is just not possible\" when the competition shows that it *is* actually quite possible.","author":{"url":"https://github.com/lll000111","@type":"Person","name":"lll000111"},"datePublished":"2019-07-20T10:43:52Z","url":"https://github.com/microsoft/TypeScript/issues/32497"}
| 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:ce8d254e-2075-80e2-231c-63409af8bce0 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 9272:1CCECA:8604EB:B3378C:6B01BB49 |
| html-safe-nonce | e140e08ba0e4a6a68f830d88dfc9b865f42e40f61df6553aed18baa0a31dd335 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5MjcyOjFDQ0VDQTo4NjA0RUI6QjMzNzhDOjZCMDFCQjQ5IiwidmlzaXRvcl9pZCI6IjI2MDY3MjAyMTkwMTE0NjQwMDkiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 44fd9e5631c8ebcfce24e479f7152aa05e057848d9c13c245c1798aa19639ab5 |
| hovercard-subject-tag | issue:470661663 |
| 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/32497/issue_layout |
| twitter:image | https://opengraph.githubassets.com/480e7a73c67f089b9cb1fbabafc799d10a3f2a436662ca027b91242b8bc83e21/microsoft/TypeScript/issues/32497 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/480e7a73c67f089b9cb1fbabafc799d10a3f2a436662ca027b91242b8bc83e21/microsoft/TypeScript/issues/32497 |
| og:image:alt | I realize there are a number of related issues. However, some where about ReadonlyArray, some didn't see directly related at all even though it was pointed there. Also, this is a much smaller examp... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | lll000111 |
| hostname | github.com |
| expected-hostname | github.com |
| None | cc5dfdd32f62ebd3223cab3af3aa2b268d3605218237326a48a99d1efd89495c |
| 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 | 8c77c9e8439031ad54c75a250694842892f693e1 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width