Title: [Control flow] Array filter with typeguard works differently following array initial value · Issue #60108 · microsoft/TypeScript · GitHub
Open Graph Title: [Control flow] Array filter with typeguard works differently following array initial value · Issue #60108 · microsoft/TypeScript
X Title: [Control flow] Array filter with typeguard works differently following array initial value · Issue #60108 · microsoft/TypeScript
Description: 🔎 Search Terms filter + typeguard + control flow + strictNullChecks 🕗 Version & Regression Information This is the behavior in every version I tried, and I reviewed the FAQ for entries about filter + typeguards + control flow + strictNul...
Open Graph Description: 🔎 Search Terms filter + typeguard + control flow + strictNullChecks 🕗 Version & Regression Information This is the behavior in every version I tried, and I reviewed the FAQ for entries about filter...
X Description: 🔎 Search Terms filter + typeguard + control flow + strictNullChecks 🕗 Version & Regression Information This is the behavior in every version I tried, and I reviewed the FAQ for entries about fi...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/60108
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"[Control flow] Array filter with typeguard works differently following array initial value","articleBody":"### 🔎 Search Terms\r\n\r\nfilter + typeguard + control flow + strictNullChecks\r\n\r\n### 🕗 Version & Regression Information\r\n\r\n- This is the behavior in every version I tried, and I reviewed the FAQ for entries about filter + typeguards + control flow + strictNullChecks\r\n\r\n### ⏯ Playground Link\r\n\r\nhttps://www.typescriptlang.org/play/?strict=false&noImplicitAny=false&strictFunctionTypes=false&strictPropertyInitialization=false&strictBindCallApply=false&noImplicitThis=false&noImplicitReturns=false&alwaysStrict=false&esModuleInterop=false&declaration=false&target=0&jsx=0&module=0&ts=5.6.2&experimentalDecorators=false&emitDecoratorMetadata=false#code/MYewdgzgLgBATgQwO4C4bTgSzAcwNoC6MAPjAIJyICeAPBtjiTGAK4A2bAfDALwyEBuAFBDQkWADNMbKAFM4sgCZp6uQr3jIAdFJnyAFPuiyADiqhZcTVhwCU50zEwR0Fhr25QqJ2SAmvHHiCYAHJVHBDbGAB6aJgAaQB5ETFoV0scACVkcwz1UgpqOjcrUhsuDTwAIhM2EBMqgmFRcDTwgDFpOQVldIZ1PnDspB0ugyM5Mz7S5nY2ewCTJxdwjxgvHz9F3mCwkoio2ITklvFZjmHc-qICygRaVbK57j48cqaU1thyzr0eq7URD45WGoz+hmMU0e53mDiWzmmjB4nm8vn8kJ2fD2GUiMTiiXinzO5QACmwWBAAMr7S6I-LkO4PfbWZ6VGp1BoAGhhH1OaVJ5Kp+1+3SUAPwQJhZIp1IyoN03Qhk3FLLscOWiLWGzR2yCWPCuKOBKEQA\r\n\r\n### 💻 Code\r\n\r\n```ts\r\nconst raw: string[] | (string | null)[] = [];\r\n\r\nconst filtered: string[] = raw.filter((step: string | null): step is string => typeof step === 'string') // KO\r\n// Type '(string | null)[]' is not assignable to type 'string[]'.\r\n```\r\n\r\n```ts\r\nconst raw: string[] |(string | null)[] = [\"plop\", null];\r\n\r\nconst filtered: string[] = raw.filter((step: string | null): step is string => typeof step === 'string') // OK no error\r\n```\r\n\r\n### 🙁 Actual behavior\r\n\r\nThere is a strange difference in control flow when you filter nullish values in an array regarding the declaration of that array. This is definitely a edge case. The type `string[] | (string | null)[]` comes from an union type in my case.\r\n\r\n### 🙂 Expected behavior\r\n\r\nNo difference and typeguard should work (regardless the fact it is valid).\r\n\r\n### Additional information about the issue\r\n\r\nI first thought it was a regression due to #57465 but I tested it on several version. The issue has always been here. I looked into a lot of issues about typeguards and filters but unfortunately, could not find one similar to my issue :cry: \r\n\r\nThanks anyway for the great job","author":{"url":"https://github.com/paulsouche","@type":"Person","name":"paulsouche"},"datePublished":"2024-09-30T17:25:25Z","url":"https://github.com/microsoft/TypeScript/issues/60108"}
| 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:c14a7eb3-a4ac-a5c1-cacb-0657590ae5c7 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | A8D0:31B4CC:B21068:EBDCB1:6B00EB41 |
| html-safe-nonce | 55f4d7cd16d094753c898862852417ae3558e1cebfea8971a04e3e5d9aae4efb |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBOEQwOjMxQjRDQzpCMjEwNjg6RUJEQ0IxOjZCMDBFQjQxIiwidmlzaXRvcl9pZCI6IjU3MDMxMDgxMjg2MzYyMDE3OTMiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 4a01292df837c745e45741d831f1c545f14b52dff0a938ff6c1e7450c801b089 |
| hovercard-subject-tag | issue:2557261310 |
| 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/60108/issue_layout |
| twitter:image | https://opengraph.githubassets.com/5117511e65d3399af1e5fa4516ac67f6134f06aa3124be0262b99c6286b753fd/microsoft/TypeScript/issues/60108 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/5117511e65d3399af1e5fa4516ac67f6134f06aa3124be0262b99c6286b753fd/microsoft/TypeScript/issues/60108 |
| og:image:alt | 🔎 Search Terms filter + typeguard + control flow + strictNullChecks 🕗 Version & Regression Information This is the behavior in every version I tried, and I reviewed the FAQ for entries about filter... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | paulsouche |
| 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