Flow: flat/flatMap not available

Created on 26 Jan 2019  路  7Comments  路  Source: facebook/flow

There is currently no open issue describing the lack of flatMap.

As this is a stage 3 proposal, it should be supported by Flow.

Library definitions enhancement

Most helpful comment

as a workaroung im using identity function as mapper in flatMap

const nums: Array<number> = [[1], [2], [3]].flatMap(x =>x);

All 7 comments

Reopened #6602 which has more info.

Reopened #6602 which has more info.

6602 asks how to implement such types in user-land instead of raising the issue that types for Array#flat/ Arrray#flatMap are missing in the standard Flow type definitions. The two are related, but different issues.

They are not different, Flow types are Flow types. It's really obvious that it should be in the Flow lib since the question is for an official JS function (the linked issue even links to the standard — well, to the proposal, it isn't in https://tc39.github.io/ecma262/ yet, it still is stage 3). The linked issue has a suggestion how to actually do it (better than any I'd say?):

declare export function flatten<T, X>(array?: ?Array<Array<T> | X>): Array<T | X>;

Anyway, I only linked it for those suggestions, they can be used as-is exactly, and yes I mean putting them into the Flow lib definitions.

PS: For a long moment I confused this with the far, far more onerous Flow type for extracting function arguments which covers almost ten lines of Flow definition for one thing.... but if the above line does not work for everybody that's probably the route for flatMap too, describing several levels and concatenating them with &

See #6948 for an attempt at this.

anyone progress on flat?

as a workaroung im using identity function as mapper in flatMap

const nums: Array<number> = [[1], [2], [3]].flatMap(x =>x);

I added support for common patterns (that are possible to type) for .flat:

https://flow.org/try/#0MYewdgzgLgBGCuBbCAuGBBATpghgTwB4FEAjAU0wD4YBeGAbXoEYBdAGgYCZ2GBmFlgDoAZgBscUABQBKANxA

Was this page helpful?
0 / 5 - 0 ratings