typed-typings / npm-lodash

The type definition for https://github.com/lodash/lodash
MIT License
16 stars 22 forks source link

flatten (deep) #32

Open KiaraGrouwstra opened 7 years ago

KiaraGrouwstra commented 7 years ago

Hi @blakeembrey. :D

I started checking out the lodash typings looking for inspiration to improve the ramda ones. Definitely learned there! Can't say I envy all the extra lodash wrappers though, and here I thought we had it bad with currying!

So one point I'd still had trouble getting to type correctly there was flatten, for which I tried out if the lodash definition (deep case) might be of help. I tried to test like what'd be as follows in the lodash case:

let numbers: number[] = _.flatten([1, 2, [3, 4], 5, [6, [7, 8, [9, [10, 11], 12]]]], true);

For me (nightly TS 2.2) this wasn't quite type-checking normally yet; curious if it was functional on your side or if we're in the same boat here for now.

Just figured since we're largely typing similar functions having linked issues up might be of use. That said, there might still be some existing typings in our ramda one that might be of use in further typing lodash as well.

blakeembrey commented 7 years ago

Hey! Note that these lodash definitions, I don't think, are very well maintained and DefinitelyTyped is likely a better place to get them right now. For that specific use-case, I think you could try https://github.com/blakeembrey/array-flatten/blob/master/array-flatten.d.ts though I'm not 100% how well that works on the latest TypeScript versions either.

unional commented 7 years ago

Sorry, currently I was very busy at work so don't have time updating the definitions.

Hopefully when redirect works we can consolidate the work with DT and get things going for the better. 🌷

KiaraGrouwstra commented 7 years ago

Hah, quite the coincidence you already had something just like this. :) Currently the following, based on your .d.ts there, is giving me the same issue in TS Playground (currently uses 2.1.4-insiders.20161201 -- wish it allowed easily switching versions to experiment!):

declare function flatten<T>(array: NestedArray<T>): T[];
interface NestedArray <T> {
  [index: number]: T | NestedArray<T>;
  length: number;
}
let numbers: number[] = flatten([1, 2, [3, 4], 5, [6, [7, 8, [9, [10, 11], 12]]]]);
// Type '(number | (number | (number | (number | number[])[])[])[])[]' is not assignable to type 'number[]'.
//  Type 'number | (number | (number | (number | number[])[])[])[]' is not assignable to type 'number'.
//    Type '(number | (number | (number | number[])[])[])[]' is not assignable to type 'number'.

That said, priority here is in the order of 'super not urgent' to me. Was just wondering pretty much.