Title: Can no longer pass Array.isArray to function accepting type guard to narrow type, e.g. array.filter(Array.isArray) · Issue #41610 · microsoft/TypeScript · GitHub
Open Graph Title: Can no longer pass Array.isArray to function accepting type guard to narrow type, e.g. array.filter(Array.isArray) · Issue #41610 · microsoft/TypeScript
X Title: Can no longer pass Array.isArray to function accepting type guard to narrow type, e.g. array.filter(Array.isArray) · Issue #41610 · microsoft/TypeScript
Description: TypeScript Version: 4.1.2 Search Terms: Code Playground const aggregatedResults: (number[] | null | 'loading')[] = [[1, 2], null, [3, 4], 'loading', [5, 6]] const onlyTheArrays = aggregatedResults.filter
Open Graph Description: TypeScript Version: 4.1.2 Search Terms: Code Playground const aggregatedResults: (number[] | null | 'loading')[] = [[1, 2], null, [3, 4], 'loading', [5, 6]] const onlyTheArrays = aggregatedResults....
X Description: TypeScript Version: 4.1.2 Search Terms: Code Playground const aggregatedResults: (number[] | null | 'loading')[] = [[1, 2], null, [3, 4], 'loading', [5, 6]] const onlyTheArrays = ag...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/41610
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Can no longer pass Array.isArray to function accepting type guard to narrow type, e.g. array.filter(Array.isArray)","articleBody":"\u003c!-- 🚨 STOP 🚨 STOP 🚨 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!--\r\nPlease try to reproduce the issue with the latest published version. It may have already been fixed.\r\nFor npm: `typescript@next`\r\nThis is also the 'Nightly' version in the playground: http://www.typescriptlang.org/play/?ts=Nightly\r\n-->\r\n**TypeScript Version:** 4.1.2\r\n\r\n\u003c!-- Search terms you tried before logging this (so others can find this issue more easily) -->\r\n**Search Terms:**\r\n\r\n**Code**\r\n\r\n[Playground](https://www.typescriptlang.org/play?target=7#code/MYewdgzgLgBAhgcwQJwKYLlVATASqiAVwBsoIAuGACjEIFsAjVZAbQF0YAfGW44rmAHJiIONgCWYBIICU7GAF4YLFgEYANDABMbTb2KaWAZk0AWXUJFjJ0wwFZNANjZsAUKEixwxAJ4AVAAtUAEFkZDgfCEV4JDQMLDwCEjIAOgAzcVJmAB5aRmZ2AD4qUPCfFPEIUoiZIA)\r\n\r\n```ts\r\nconst aggregatedResults: (number[] | null | 'loading')[] = [[1, 2], null, [3, 4], 'loading', [5, 6]]\r\nconst onlyTheArrays = aggregatedResults.filter\u003cnumber[]>(Array.isArray) // number[][]\r\n```\r\nmaking it easy to chain with more calls, e.g. `.flat()`.\r\n`number[]` needed to be passed so the end result was not `any[]`, but it was type-safe, passing e.g. `boolean[]` would give an error.\r\n\r\n**Expected behavior:**\r\n\r\n`Array.isArray` as a native type guard function should be composable with type-guard accepting functions like `array.filter()`. Those functions need to be able to pluck the narrowed type out of the signature.\r\n\r\nThe above code worked in TS 4.0.\r\n\r\n**Actual behavior:**\r\n\r\nIn TS 4.2 it seems like the signature of `Array.isArray` was changed and now has a conditional return type, which seems to make it impossible to pass it to a type-guard accepting function like `array.filter()`. The above code now gives an error ([Playground](https://www.typescriptlang.org/play?target=7&ts=4.1.0-beta#code/MYewdgzgLgBAhgcwQJwKYLlVATASqiAVwBsoIAuGACjEIFsAjVZAbQF0YAfGW44rmAHJiIONgCWYBIICU7GAF4YLFgEYANDABMbTb2KaWAZk0AWXUJFjJ0wwFZNANjZsAUKEixwxAJ4AVAAtUAEFkZDgfCEV4JDQMLDwCEjIAOgAzcVJmAB5aRmZ2AD4qUPCfFPEIUoiZIA)):\r\n\r\n```\r\nArgument of type '\u003cT>(arg: {} | T) => arg is T extends readonly any[] ? unknown extends T ? never : readonly any[] : any[]' is not assignable to parameter of type '(value: number[] | \"loading\" | null, index: number, array: (number[] | \"loading\" | null)[]) => value is number[]'.\r\n Type predicate 'arg is any[] | readonly any[]' is not assignable to 'value is number[]'.\r\n Type 'any[] | readonly any[]' is not assignable to type 'number[]'.\r\n The type 'readonly any[]' is 'readonly' and cannot be assigned to the mutable type 'number[]'.\r\n```\r\n\r\n`array.filter()` can't pluck the narrowed type out of the signature anymore because of the complex conditional type.\r\n\r\n**Related Issues:** I think this was broken by https://github.com/microsoft/TypeScript/pull/39258\r\n","author":{"url":"https://github.com/felixfbecker","@type":"Person","name":"felixfbecker"},"datePublished":"2020-11-20T10:56:16Z","url":"https://github.com/microsoft/TypeScript/issues/41610"}
| 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:29e7a834-443c-b060-2466-b35778e9ec20 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | C332:EA06E:3331AE6:4119FDF:6B01BE7C |
| html-safe-nonce | bb98291acbe29a7c1c84f282a6775a6600b00dc66b4c6098cfe78facc153e98c |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDMzMyOkVBMDZFOjMzMzFBRTY6NDExOUZERjo2QjAxQkU3QyIsInZpc2l0b3JfaWQiOiI0MjUxMjcxMzM1NjEyODkwNzQ4IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | e63e2bf07e6e751ede47d8d876c362f531b18ed7dfd00d5f09785c53ed528b07 |
| hovercard-subject-tag | issue:747392818 |
| 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/41610/issue_layout |
| twitter:image | https://opengraph.githubassets.com/c3036c6c4ff7d4c278c706f9dc3f84c25b1e6b6675de20f716862ab9d1315205/microsoft/TypeScript/issues/41610 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/c3036c6c4ff7d4c278c706f9dc3f84c25b1e6b6675de20f716862ab9d1315205/microsoft/TypeScript/issues/41610 |
| og:image:alt | TypeScript Version: 4.1.2 Search Terms: Code Playground const aggregatedResults: (number[] | null | 'loading')[] = [[1, 2], null, [3, 4], 'loading', [5, 6]] const onlyTheArrays = aggregatedResults.... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | felixfbecker |
| 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