Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unlimited Array.prototype.flat depth #55686

Closed
wants to merge 4 commits into from

Conversation

PKrasinski
Copy link

Fixes #55685

The old Array.prototype.flat definition supports depth only up to 21. Over 21 return wrong type.
This solution allow call flat method with unlimited depth parameter.

const foo = [ [ ['string' ] ] ]
const flatFoo = foo.flat(1000000) // string[]

Performance (checked with this code) is the same or better in some cases.

The only limitation of this solution is that for arrays deeper then 999 levels unknown[] type is returned.

[
  [
    [
       // 997 levels
    ]
  ]
].flat(1000) // unknown[]

@typescript-bot typescript-bot added the For Uncommitted Bug PR for untriaged, rejected, closed or missing bug label Sep 8, 2023
@PKrasinski
Copy link
Author

@microsoft-github-policy-service agree

@MartinJohns
Copy link
Contributor

Introducing new global types with generic names and increasing the complexity of the types enormously for a highly questionable use case. :-/

@PKrasinski
Copy link
Author

PKrasinski commented Sep 8, 2023

@MartinJohns Yep, probably you are right. I was just looking for the way how to type safe recurrent functions so I checked how it looks like in flat method declaration and I was a bit surprised that solution is using hard-coded array and returns wrong type when the depth is higher. So I decided to write my own implementation just for fun 😄 I could avoid new global types in this solution. I also know that probably I solved the problem that nobody had.

@DanielRosenwasser
Copy link
Member

I think for reasons discussed here and elsewhere, I would encourage people who need this to include their own local augmentations to the global scope.

@PKrasinski PKrasinski closed this Sep 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
For Uncommitted Bug PR for untriaged, rejected, closed or missing bug
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Array.prototype.flat limited depth to 21
4 participants