Title: Trying to flatten an array with unknown depth causes a type error · Issue #44408 · microsoft/TypeScript · GitHub
Open Graph Title: Trying to flatten an array with unknown depth causes a type error · Issue #44408 · microsoft/TypeScript
X Title: Trying to flatten an array with unknown depth causes a type error · Issue #44408 · microsoft/TypeScript
Description: Bug Report Trying to flatten an array with unknown depth causes a type error. 🔎 Search Terms flat Infinity 🕗 Version & Regression Information Errors in version 4.2.4 This changed between versions 3.6.2 and 3.7.5 (but probably just becaus...
Open Graph Description: Bug Report Trying to flatten an array with unknown depth causes a type error. 🔎 Search Terms flat Infinity 🕗 Version & Regression Information Errors in version 4.2.4 This changed between versions 3...
X Description: Bug Report Trying to flatten an array with unknown depth causes a type error. 🔎 Search Terms flat Infinity 🕗 Version & Regression Information Errors in version 4.2.4 This changed between versio...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/44408
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Trying to flatten an array with unknown depth causes a type error","articleBody":"# Bug Report\r\n\r\nTrying to flatten an array with unknown depth causes a type error.\r\n\r\n### 🔎 Search Terms\r\n\r\n\u003c!--\r\n What search terms did you use when trying to find an existing bug report?\r\n List them here so people in the future can find this one more easily.\r\n--\u003e\r\n * `flat`\r\n * `Infinity`\r\n\r\n### 🕗 Version \u0026 Regression Information\r\n\r\n- Errors in version 4.2.4\r\n- This changed between versions 3.6.2 and 3.7.5 (but probably just because the type definitions aren't supported prior)\r\n\r\nThe PR https://github.com/microsoft/TypeScript/pull/32131 provides a definition for `FlatArray` that has this issue.\r\n\r\n### ⏯ Playground Link\r\n\r\n[Playground link with relevant code](https://www.typescriptlang.org/play?target=99\u0026ts=4.3.2\u0026filetype=ts#code/PTAEDMEsCcGcBdQBMCmUB2LQENSYSkqADaQKgDuk8AFjtAEbXTbQCeyKADrQFDxsuWAHIoCSADwAVAHygAvKAAUU0AB9Qo8dJkBKANoBdANy9eAYwD26cq2gAuTWPiEJ6AK4BbBimhzF+gCMADT6AEyhAMyGMaa8ICSW5tjExGzBoLRkoEyI4NiQxLAQltCgnlhKAG6+sJDWoAAsAHRhzY268cBdlNR02OigvtCloADkAETQKObu0BNjoNnT4L4o6OaEyDAz8Gmgo5DoSDvmexxHS-DFlhSDAkI46OiW8Njw9ejNFtbk07CODzeXxGBT0aDNcDEd5KACS6Aw1DYujiCTYlncoGSgyQlhwoAAju5IOYANaUUqk1gY444YrgSypW4An42RD-MKArw+aCgxRKOx0p5sIy6SHQ+BwhFHJEoswJBjuPKjWCWCqgabYVWDBq0LBSQQoADK5mgkB4oC40LYAHMRu5aVlirixOgxnkCsQMorEOjMdihp53BKsNQegwOO46ugbZkaFgGUyqDHOIiPrqaO8lsUUETIFUUutEPA8XqDphLSMqpBUERLmWEAMkKwiKQGCx2PYejR4PAuACQDa+u4GM0rJ5gJ4SSNVeB4MADUITWaeMAGMRLAxgEgABwoACc4AAbPuAKyBfcX7CRRoAdgYDECp9PDH3kR32FPAAYv0fwPu0GwB9b1vHdgDbcDIFHMQwi-C9mjsbA2GaJBmmuABiAAZMJAn4Q1QAAMQlABBaAWDYMIJFI6AMgAEW4WghgADxcY5iiBHl-FAABvXhQH40AJlxTAJkcajgj4gSphmOZRNAajmNYpBigAJRQbBhLSaikIkI5VjKeFMGgaiZEkgSBIAfkIkiyKQyjDN8cTQH0ABaEJQC-DJ3IiUBIgyRoMlPDIjwyW8Mh3DJ9y8zzQECdzAh8wI-NigLYqC2KQtisLYoi2KotAWDDH0eieBoQxTPM8yxLI3gAF9ioYugUBY9ZlNANzQCsoTrBQCZQEcaTZnmEx5TAZIozEfEHiwYZSkcSZpiGhZsw1NA1g2LYTkW84DjKI4tt2fZLmoG47kyfCBheN50y+PDHikZwiPeHQwSe+BtIoiQtBcSRZAyabLHAUB4TTNgZCAA)\r\n\r\n### 💻 Code\r\n\r\n```ts\r\n// first define a nested list with arbitrary depth\r\ntype Nested\u003cT\u003e = (T | Nested\u003cT\u003e)[];\r\n\r\nconst arr: Nested\u003cnumber\u003e = [1,[2,[3]]];\r\n\r\n// locally, this bit fails for me (version 4.2.4)\r\n//\r\n// with an error '\"recur\"' is referenced directly or indirectly in its own type annotation.\r\nconst res: number[] = arr.flat(Infinity);\r\n\r\n// you can do a quick workaround as follows:\r\nconst res2: number[] = (arr as any[]).flat(Infinity);\r\n\r\n// but for some reason on the TypeScript playground this doesn't fail, but you can emulate it\r\n// by using the following definition that is equivalent to the one provided in the standard library:\r\n// https://github.com/microsoft/TypeScript/blob/d8e9f6951919a347bb155b938a5006f9efabb778/lib/lib.es2019.array.d.ts#L21\r\ntype FlatArray2\u003cArr, Depth extends number\u003e = {\r\n \"done\": Arr,\r\n \"recur\": Arr extends ReadonlyArray\u003cinfer InnerArr\u003e\r\n ? FlatArray2\u003cInnerArr, [-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20][Depth]\u003e\r\n : Arr\r\n}[Depth extends -1 ? \"done\" : \"recur\"];\r\n\r\n// causes a type error: '\"recur\"' is referenced directly or indirectly in its own type annotation.\r\ntype TestFlat\u003cT\u003e = FlatArray2\u003cNested\u003cT\u003e, typeof Infinity\u003e\r\n```\r\n\r\n### 🙁 Actual behavior\r\n\r\nThe code errors with `'\"recur\"' is referenced directly or indirectly in its own type annotation.`\r\n\r\n### 🙂 Expected behavior\r\n\r\nThe code to pass typechecking.\r\n","author":{"url":"https://github.com/GuiltyDolphin","@type":"Person","name":"GuiltyDolphin"},"datePublished":"2021-06-03T11:07:04.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":3},"url":"https://github.com/44408/TypeScript/issues/44408"}
| 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:0880a025-2af5-0265-fec8-7fa383d915e5 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | BDD0:BDBC8:224C3D:2F42DE:6A6267F9 |
| html-safe-nonce | 291ef30bc7674cd4af4bb2e5643699d5f5cf0b79f49998b7803f15d118ef5be6 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCREQwOkJEQkM4OjIyNEMzRDoyRjQyREU6NkE2MjY3RjkiLCJ2aXNpdG9yX2lkIjoiNTM5ODA2MDM2OTkzOTk0OTU2MSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 03a853f4b914c98e8563f9c1ce6be49750bf27c6724303447efe4085388a1db2 |
| hovercard-subject-tag | issue:910383125 |
| 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/44408/issue_layout |
| twitter:image | https://opengraph.githubassets.com/859810921e211e506fad4c28c5b7b3073b711dce24441d5fde543811c24c44c7/microsoft/TypeScript/issues/44408 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/859810921e211e506fad4c28c5b7b3073b711dce24441d5fde543811c24c44c7/microsoft/TypeScript/issues/44408 |
| og:image:alt | Bug Report Trying to flatten an array with unknown depth causes a type error. 🔎 Search Terms flat Infinity 🕗 Version & Regression Information Errors in version 4.2.4 This changed between versions 3... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | GuiltyDolphin |
| hostname | github.com |
| expected-hostname | github.com |
| None | 194bddaed53c0eb07047629c520853f4208e77a17ff57428346485cc202e39f2 |
| 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 | c2862cf2db52f0c7f6cba3de21ffeeafe7c5456e |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width