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

Array.prototype.flat returns mixed on nested arrays #8999

Open
FezVrasta opened this issue Jan 30, 2023 · 2 comments
Open

Array.prototype.flat returns mixed on nested arrays #8999

FezVrasta opened this issue Jan 30, 2023 · 2 comments
Labels
Library definitions Issues or pull requests about core library definitions

Comments

@FezVrasta
Copy link
Contributor

FezVrasta commented Jan 30, 2023

Flow version: 0.198.2

Expected behavior

The resulting array should be all numbers.

Actual behavior

The problem seems to be the type definition itself, why is it like that?
https://github.com/facebook/flow/blob/main/lib/core.js#L832

@darichey
Copy link
Member

darichey commented Jan 30, 2023

Giving a general type to flat requires some type machinery not (yet) available in Flow. The simple case is somewhat easy, and depending on your use case, you might be able to work around the issue by defining some overloaded function like:

declare function flatten<T>(arr: $ReadOnlyArray<$ReadOnlyArray<T>>, depth: 2): Array<T>;
declare function flatten<T>(arr: $ReadOnlyArray<$ReadOnlyArray<$ReadOnlyArray<T>>>, depth: 3): Array<T>;

But this won't work for the case of different levels of nesting and stopping at a certain depth (e.g., [[1], [[2]], [[[3]]]].flat(2)).

Some previous discussion:
#6602
#7397

@FezVrasta
Copy link
Contributor Author

Could support for a couple levels be added to the lib though? It should cover most of the use cases

@darichey darichey added Library definitions Issues or pull requests about core library definitions and removed bug needs triage labels Feb 3, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Library definitions Issues or pull requests about core library definitions
Projects
None yet
Development

No branches or pull requests

2 participants