Skip to content

Commit

Permalink
Improve Flow libdefs (#845)
Browse files Browse the repository at this point in the history
* Improve Flow libdefs

* typo

* Add any typeParam to make improve intent of it not being on the Iterable interface

* move filter to Iterable.
This has some downsides but matches flows libdef for Array.filter.
If this is the best way should be discussed on flow. We just match the main libdef  for filter.
  • Loading branch information
marudor authored and leebyron committed Apr 18, 2016
1 parent f4d4cc8 commit 09e049b
Showing 1 changed file with 46 additions and 24 deletions.
70 changes: 46 additions & 24 deletions type-definitions/immutable.js.flow
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@
*/
type ESIterable<T> = $Iterable<T,void,void>;

declare class Iterable<K, V> extends _Iterable<K, V, typeof KeyedIterable, typeof IndexedIterable, typeof SetIterable> {}
declare class Iterable<K, V> extends _Iterable<K, V, typeof KeyedIterable, typeof IndexedIterable, typeof SetIterable> {}

declare class _Iterable<K,V, KI, II, SI> {
declare class _Iterable<K, V, KI, II, SI> {
static Keyed: KI;
static Indexed: II;
static Set: SI;
Expand All @@ -44,16 +44,16 @@ declare class _Iterable<K,V, KI, II, SI> {

equals(other: Iterable<K,V>): boolean;
hashCode(): number;
get(key: K): V;
get<V_>(key: K, notSetValue: V_): V|V_;
get(key: K): ?V;
has(key: K): boolean;
includes(value: V): boolean;
contains(value: V): boolean;
first(): V;
last(): V;

getIn<T>(searchKeyPath: ESIterable<any>, notSetValue: T): T;
getIn<T>(searchKeyPath: ESIterable<any>): ?T;
getIn<T>(searchKeyPath: ESIterable<any>): T;
hasIn(searchKeyPath: ESIterable<any>): boolean;

toJS(): any;
Expand All @@ -78,16 +78,6 @@ declare class _Iterable<K,V, KI, II, SI> {
valueSeq(): IndexedSeq<V>;
entrySeq(): IndexedSeq<[K,V]>;

filter(
predicate: (value: V, key: K, iter: this) => mixed,
context?: any
): this;

filterNot(
predicate: (value: V, key: K, iter: this) => mixed,
context?: any
): this;

reverse(): this;
sort(comparator?: (valueA: V, valueB: V) => number): this;

Expand Down Expand Up @@ -120,6 +110,16 @@ declare class _Iterable<K,V, KI, II, SI> {
flatten(depth?: number): /*this*/Iterable<any,any>;
flatten(shallow?: boolean): /*this*/Iterable<any,any>;

filter(
predicate: (value: V, key: K, iter: this) => mixed,
context?: any
): this;

filterNot(
predicate: (value: V, key: K, iter: this) => mixed,
context?: any
): this;

reduce<R>(
reducer: (reduction: R, value: V, key: K, iter: this) => R,
initialReduction?: R,
Expand All @@ -139,25 +139,26 @@ declare class _Iterable<K,V, KI, II, SI> {
count(predicate?: (value: V, key: K, iter: this) => mixed, context?: any): number;
countBy<G>(grouper: (value: V, key: K, iter: this) => G, context?: any): Map<G,number>;

find(
predicate: (value: V, key: K, iter: this) => mixed,
context?: any,
): ?V;
find<V_>(
predicate: (value: V, key: K, iter: this) => mixed,
context: any,
notSetValue: V_
): V|V_;
find<V_>(

findLast(
predicate: (value: V, key: K, iter: this) => mixed,
context?: any,
): ?V;

findLast<V_>(
predicate: (value: V, key: K, iter: this) => mixed,
context: any,
notSetValue: V_
): V|V_;
findLast<V_>(
predicate: (value: V, key: K, iter: this) => mixed,
context?: any,
): ?V;


findEntry(predicate: (value: V, key: K, iter: this) => mixed): ?[K,V];
findLastEntry(predicate: (value: V, key: K, iter: this) => mixed): ?[K,V];
Expand Down Expand Up @@ -214,6 +215,9 @@ declare class KeyedIterable<K,V> extends Iterable<K,V> {
mapper: (value: V, key: K, iter: this) => ESIterable<[K_,V_]>,
context?: any
): /*this*/KeyedIterable<K_,V_>;

flatten(depth?: number): /*this*/KeyedIterable<any,any>;
flatten(shallow?: boolean): /*this*/KeyedIterable<any,any>;
}

declare class IndexedIterable<T> extends Iterable<number,T> {
Expand Down Expand Up @@ -319,6 +323,9 @@ declare class IndexedIterable<T> extends Iterable<number,T> {
mapper: (value: T, index: number, iter: this) => ESIterable<U>,
context?: any
): /*this*/IndexedIterable<U>;

flatten(depth?: number): /*this*/IndexedIterable<any>;
flatten(shallow?: boolean): /*this*/IndexedIterable<any>;
}

declare class SetIterable<T> extends Iterable<T,T> {
Expand All @@ -342,9 +349,12 @@ declare class SetIterable<T> extends Iterable<T,T> {
mapper: (value: T, value: T, iter: this) => ESIterable<U>,
context?: any
): /*this*/SetIterable<U>;

flatten(depth?: number): /*this*/SetIterable<any>;
flatten(shallow?: boolean): /*this*/SetIterable<any>;
}

declare class Collection<K,V> extends _Iterable<K,V, typeof KeyedCollection, typeof IndexedCollection, typeof SetCollection> {
declare class Collection<K,V> extends _Iterable<K,V, typeof KeyedCollection, typeof IndexedCollection, typeof SetCollection> {
size: number;
}

Expand Down Expand Up @@ -390,7 +400,7 @@ declare class SetSeq<T> extends Seq<T,T> mixins SetIterable<T> {
}

declare class List<T> extends IndexedCollection<T> {
static <T>(iterable?: ESIterable<T>): List<T>;
static (iterable?: ESIterable<T>): List<T>;

static isList(maybeList: any): boolean;
static of<T>(...values: T[]): List<T>;
Expand Down Expand Up @@ -439,7 +449,6 @@ declare class List<T> extends IndexedCollection<T> {
asImmutable(): this;

// Overrides that specialize return types

map<M>(
mapper: (value: T, index: number, iter: this) => M,
context?: any
Expand All @@ -449,15 +458,19 @@ declare class List<T> extends IndexedCollection<T> {
mapper: (value: T, index: number, iter: this) => ESIterable<M>,
context?: any
): List<M>;

flatten(depth?: number): /*this*/List<any>;
flatten(shallow?: boolean): /*this*/List<any>;
}

declare class Map<K,V> extends KeyedCollection<K,V> {
static <K, V>(): Map<K, V>;
static <V>(obj?: {[key: string]: V}): Map<string, V>;
static <K, V>(iterable?: ESIterable<[K,V]>): Map<K, V>;

static isMap(maybeMap: any): boolean;

set<K_,V_>(key: K_, value: V_): Map<K|K_,V|V_>;
set<K_, V_>(key: K_, value: V_): Map<K|K_, V|V_>;
delete(key: K): this;
remove(key: K): this;
clear(): this;
Expand Down Expand Up @@ -516,6 +529,9 @@ declare class Map<K,V> extends KeyedCollection<K,V> {
mapper: (key: K, value: V, iter: this) => K_,
context?: any
): Map<K_,V>;

flatten(depth?: number): /*this*/Map<any,any>;
flatten(shallow?: boolean): /*this*/Map<any,any>;
}

// OrderedMaps have nothing that Maps do not have. We do not need to override constructor & other statics
Expand Down Expand Up @@ -555,6 +571,9 @@ declare class Set<T> extends SetCollection<T> {
mapper: (value: T, value: T, iter: this) => ESIterable<M>,
context?: any
): Set<M>;

flatten(depth?: number): /*this*/Set<any>;
flatten(shallow?: boolean): /*this*/Set<any>;
}

// OrderedSets have nothing that Sets do not have. We do not need to override constructor & other statics
Expand Down Expand Up @@ -592,6 +611,9 @@ declare class Stack<T> extends IndexedCollection<T> {
mapper: (value: T, index: number, iter: this) => ESIterable<U>,
context?: any
): Stack<U>;

flatten(depth?: number): /*this*/Stack<any>;
flatten(shallow?: boolean): /*this*/Stack<any>;
}

declare function Range(start?: number, end?: number, step?: number): IndexedSeq<number>;
Expand Down

0 comments on commit 09e049b

Please sign in to comment.