-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
feat: add flat/flatMap #6948
feat: add flat/flatMap #6948
Conversation
It's not standardized yet and possibly |
It looks like they got over the concern and this is shipped in Firefox and Chrome, at least. Since it's shipped in browsers, and the proposal is pretty far along, I'm comfortable adding support for this. Unfortunately I'm not sure this is possible to accurately type in Flow. For example, with the proposed libdef:
Basically, we would want a type system feature which would allow us to say "You can call this if the As an aside, Skip lets you do this and it's really cool. I think that updating this to use |
drive-by thought: can you do anything with type Join<T> = Array<$ElementType<$ElementType<T, number>, number>>; ? |
Would it be possible to add only |
|
I'll merge this once my concerns described above are addressed. I suspect that the best we can do is |
@nmote React.ChildrenArray does flattening // A children array can be a single value...
const children: React.ChildrenArray<number> = 42;
// ...or an arbitrarily nested array.
const children: React.ChildrenArray<number> = [[1, 2], 3, [4, 5]];
// Using the `React.Children` API can flatten the array.
const array: Array<number> = React.Children.toArray(children); |
Could someone from the Flow team take a look at this PR? Not having support for standard JS features is pretty bad... |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My earlier comment still stands. This is not typesafe as written. Requesting changes to make this more explicit.
Others are welcome to open competing PRs if they do not want to wait for @keithamus to respond.
|
Here is an accurate type Dec = [-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
type Flat<A, D extends number> = {
'1': A,
'0': A extends ReadonlyArray<infer T> ? Flat<T, Dec[D]> : A
}[D extends -1 ? '1' : '0']
declare function flat<A, D extends number = 1>(array: A, depth?: D): Flat<A, D>[] If someone knows how to rewrite it in Flow, feel free to propose ideas |
I've merged #7854 which added a declaration for |
Another great example of "uhm yeah this is a standard js feature but you just can't use it and you basically have to decide between using flow or not" At least let me freaking disable certain parts of flowlib, or let me extend it locally, or whatever. The way you guys handle these kind of "don't think we can type it currently more precisely" issues is mindbogglingly frustrating. Then don't type it and just ignore it. Or use It's not like flow is unusable, no, flow actually makes 20% of javascript unusable. Correct me if I'm wrong, but my only options in these cases would be:
|
You've correctly identified your options: either work around this missing libdef or submit a PR that addresses the issues I've raised here. If you do so, feel free to cc me and I'll make sure to take a look at it quickly. |
Summary: ref: #6948 according to https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Global_Objects/Array/flat Pull Request resolved: #8237 Reviewed By: dsainati1 Differential Revision: D19156287 Pulled By: nmote fbshipit-source-id: dcbb31b8a3e64f7c4675165197e3d85013cac8ce
Summary: Currently, `.flat()` always returns `Array<mixed>` which limits its usefullness. While I don't think we can type it perfectly in the general case, I believe I have come up with something that will improve support for the common case: calling `xs.flat()` where `xs` is an arry of arrays, and no depth argument is supplied to `.flat()`. We still don't support depth arguments (unless they are the literal number `0` or `1`), and don't support calling `.flat` on an array like `[1, 2, [3, 4]]`, but at least this common pattern is supported. Related: discussion in #6948 on GitHub (#6948) Reviewed By: nmote Differential Revision: D21168223 fbshipit-source-id: cd74d9b0e87ce48fa67211f23a38b17c3069e276
Just taking a punt on types for
flat
andflatMap
. These are part of a TC39 Stage 4 proposal that has been implemented in all browsers.