-
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
flat/flatMap missing - but how would I type a flat() function anyway? #6602
Comments
I managed to get the easy cases to work using
taken from here. However, each time there is a promise somewhere it all fails. I'll have to investigate, I've seen that in many other experiments, always the Flow built-in promise definition caused problems. |
I ended up using the definition from flow-typed for iodash. |
How did this type work when it circularly references itself? I'm getting |
TS? How about Flow Playground? |
I saw that you mentioned Typescript in the opening post and thought this code is valid TS, my bad! |
My two cents: for properly typing Array.prototype.flat there might be no other way than having one general case and then typing out all the special cases up to a certain depth. For instance, flattening (Feel free to correct me) |
Is there no way to extend the core Line 196 in 1e71f23
|
Personally I'm okay with |
Related: #7397 |
Closing this again in favor of #7397, one issue is enough. I had originally confused this with another issue that required a lot more context, but I copied the single important line from this issue right here to the other one so no need to keep this open. |
Two parts to this question, because I came across the native JS function while writing my own (it's a common function in any case).
Stage 3 proposal:
Typescript uses this monster definition - each possible level gets its own definition: https://github.com/Microsoft/TypeScript/blob/master/src/lib/esnext.array.d.ts
I came across the problem of having to type a
flat()
function after I added one to my own project:This does not work. I cannot use just
T
, norArray<T>
(since at the very least I want to allow pure values as array items as well as arrays of pure values, for a nesting level of 1), nor doesT | Array<T>
seem to work even though I only have depth:1 arrays in my code.Looking at the Microsoft definition of
flat
, this looks like a nightmare function to add types to. How would I do this in my code for my own function anyway? In the global definition which one could use something similar to what Typescript has, but when I document my own code and my own function inline I can only add one single type.So another way to phrase this, when I don't write library definition types but type directly in the respective function, how can I have multiple different interfaces?
interface
in Flow seems to be for classes, I probably need different interfaces for the same function though (I don't have any classes in my functional-style code anyway).I looked for an example how it was solved for libraries like Ramda which provide a
flatten
function, found this, and they usebut the
any
route is not where I think this should end.Related(?): #4305, #60
I also tried intersections as shown here: https://stackoverflow.com/a/45843913/544779
My intersection looks like this.
but then I'm not sure about the generics parameter: Write
type FlattenFN
? But where do I get the type from when I actually use the intersection type:Flow Try Link
The text was updated successfully, but these errors were encountered: