8000 Flow type: Add special case for `.filter(Boolean)` to remove nulls. by leebyron · Pull Request #1352 · immutable-js/immutable-js · GitHub
[go: up one dir, main page]

Skip to content

Flow type: Add special case for .filter(Boolean) to remove nulls. #1352

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

Merged
merged 1 commit into from
Oct 5, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 76 additions & 9 deletions type-definitions/immutable.js.flow
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,6 @@ declare class _Collection<K, +V> /*implements ValueObject*/ {
takeWhile(predicate: (value: V, key: K, iter: this) => mixed, context?: mixed): this;
takeUntil(predicate: (value: V, key: K, iter: this) => mixed, context?: mixed): this;

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

filterNot(
predicate: (value: V, key: K, iter: this) => mixed,
context?: mixed
Expand Down Expand Up @@ -217,6 +212,12 @@ declare class KeyedCollection<K, +V> extends Collection<K, V> {
concat<KC, VC>(...iters: Array<Iterable<[KC, VC]>>): KeyedCollection<K | KC, V | VC>;
concat<KC, VC>(...iters: Array<PlainObjInput<KC, VC>>): KeyedCollection<K | KC, V | VC>;

filter(predicate: typeof Boolean): KeyedCollection<K, $NonMaybeType<V>>;
filter(
predicate: (value: V, key: K, iter: this) => mixed,
context?: mixed
): KeyedCollection<K, V>;

map<M>(
mapper: (value: V, key: K, iter: this) => M,
context?: mixed
Expand Down Expand Up @@ -371,6 +372,12 @@ declare class IndexedCollection<+T> extends Collection<number, T> {

concat<C>(...iters: Array<Iterable<C> | C>): IndexedCollection<T | C>;

filter(predicate: typeof Boolean): IndexedCollection<$NonMaybeType<T>>;
filter(
predicate: (value: T, index: number, iter: this) => mixed,
context?: mixed
): IndexedCollection<T>;

map<M>(
mapper: (value: T, index: number, iter: this) => M,
context?: mixed
Expand All @@ -396,10 +403,16 @@ declare class SetCollection<+T> extends Collection<T, T> {

concat<C>(...iters: Array<Iterable<C> | C>): SetCollection<T | C>;

// `map` and `flatMap` cannot be defined further up the hierarchy, because the
// implementation for `KeyedCollection` allows the value type to change without
// constraining the key type. That does not work for `SetCollection` - the value
// and key types *must* match.
// `filter`, `map` and `flatMap` cannot be defined further up the hierarchy,
// because the implementation for `KeyedCollection` allows the value type to
// change without constraining the key type. That does not work for
// `SetCollection` - the value and key types *must* match.
filter(predicate: typeof Boolean): SetCollection<$NonMaybeType<T>>;
filter(
predicate: (value: T, value: T, iter: this) => mixed,
context?: mixed
): SetCollection<T>;

map<M>(
mapper: (value: T, value: T, iter: this) => M,
context?: mixed
Expand Down Expand Up @@ -442,6 +455,12 @@ declare class KeyedSeq<K, +V> extends Seq<K, V> mixins KeyedCollection<K, V> {
concat<KC, VC>(...iters: Array<Iterable<[KC, VC]>>): KeyedSeq<K | KC, V | VC>;
concat<KC, VC>(...iters: Array<PlainObjInput<KC, VC>>): KeyedSeq<K | KC, V | VC>;

filter(predicate: typeof Boolean): KeyedSeq<K, $NonMaybeType<V>>;
filter(
predicate: (value: V, key: K, iter: this) => mixed,
context?: mixed
): KeyedSeq<K, V>;

map<M>(
mapper: (value: V, key: K, iter: this) => M,
context?: mixed
Expand Down Expand Up @@ -475,6 +494,12 @@ declare class IndexedSeq<+T> extends Seq<number, T> mixins IndexedCollection<T>

concat<C>(...iters: Array<Iterable<C> | C>): IndexedSeq<T | C>;

filter(predicate: typeof Boolean): IndexedSeq<$NonMaybeType<T>>;
filter(
predicate: (value: T, index: number, iter: this) => mixed,
context?: mixed
): IndexedSeq<T>;

map<M>(
mapper: (value: T, index: number, iter: this) => M,
context?: mixed
Expand Down Expand Up @@ -596,6 +621,12 @@ declare class SetSeq<+T> extends Seq<T, T> mixins SetCollection<T> {

concat<C>(...iters: Array<Iterable<C> | C>): SetSeq<T | C>;

filter(predicate: typeof Boolean): SetSeq<$NonMaybeType<T>>;
filter(
predicate: (value: T, value: T, iter: this) => mixed,
context?: mixed
): SetSeq<T>;

map<M>(
mapper: (value: T, value: T, iter: this) => M,
context?: mixed
Expand Down Expand Up @@ -663,6 +694,12 @@ declare class List<+T> extends IndexedCollection<T> {

concat<C>(...iters: Array<Iterable<C> | C>): List<T | C>;

filter(predicate: typeof Boolean): List<$NonMaybeType<T>>;
filter(
predicate: (value: T, index: number, iter: this) => mixed,
context?: mixed
): List<T>;

map<M>(
mapper: (value: T, index: number, iter: this) => M,
context?: mixed
Expand Down Expand Up @@ -849,6 +886,12 @@ declare class Map<K, +V> extends KeyedCollection<K, V> {
concat<KC, VC>(...iters: Array<Iterable<[KC, VC]>>): Map<K | KC, V | VC>;
concat<KC, VC>(...iters: Array<PlainObjInput<KC, VC>>): Map<K | KC, V | VC>;

filter(predicate: typeof Boolean): Map<K, $NonMaybeType<V>>;
filter(
predicate: (value: V, key: K, iter: this) => mixed,
context?: mixed
): Map<K, V>;

map<M>(
mapper: (value: V, key: K, iter: this) => M,
context?: mixed
Expand Down Expand Up @@ -944,6 +987,12 @@ declare class OrderedMap<K, +V> extends Map<K, V> {
concat<KC, VC>(...iters: Array<Iterable<[KC, VC]>>): OrderedMap<K | KC, V | VC>;
concat<KC, VC>(...iters: Array<PlainObjInput<KC, VC>>): OrderedMap<K | KC, V | VC>;

filter(predicate: typeof Boolean): OrderedMap<K, $NonMaybeType<V>>;
filter(
predicate: (value: V, key: K, iter: this) => mixed,
context?: mixed
): OrderedMap<K, V>;

map<M>(
mapper: (value: V, key: K, iter: this) => M,
context?: mixed
Expand Down Expand Up @@ -1001,6 +1050,12 @@ declare class Set<+T> extends SetCollection<T> {

concat<C>(...iters: Array<Iterable<C> | C>): Set<T | C>;

filter(predicate: typeof Boolean): Set<$NonMaybeType<T>>;
filter(
predicate: (value: T, value: T, iter: this) => mixed,
context?: mixed
): Set<T>;

map<M>(
mapper: (value: T, value: T, iter: this) => M,
context?: mixed
Expand Down Expand Up @@ -1036,6 +1091,12 @@ declare class OrderedSet<+T> extends Set<T> {

concat<C>(...iters: Array<Iterable<C> | C>): OrderedSet<T | C>;

filter(predicate: typeof Boolean): OrderedSet<$NonMaybeType<T>>;
filter(
predicate: (value: T, value: T, iter: this) => mixed,
context?: mixed
): OrderedSet<T>;

map<M>(
mapper: (value: T, value: T, iter: this) => M,
context?: mixed
Expand Down Expand Up @@ -1177,6 +1238,12 @@ declare class Stack<+T> extends IndexedCollection<T> {

concat<C>(...iters: Array<Iterable<C> | C>): Stack<T | C>;

filter(predicate: typeof Boolean): Stack<$NonMaybeType<T>>;
filter(
predicate: (value: T, index: number, iter: this) => mixed,
context?: mixed
): Stack<T>;

map<M>(
mapper: (value: T, index: number, iter: this) => M,
context?: mixed
Expand Down
9 changes: 9 additions & 0 deletions type-definitions/tests/immutable-flow.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,9 @@ numberList = List.of(1).flatMap((value, index, iter) => ['a'])

numberList = List.of(1).flatten()

// Specific type for filter(Boolean) which removes nullability.
numberList = nullableNumberList.filter(Boolean)

/* Map */

stringToNumber = Map()
Expand Down Expand Up @@ -333,6 +336,12 @@ stringToNumber = Map({'a': 1}).mapKeys((key, value, iter) => 1)

anyMap = Map({'a': 1}).flatten()

var stringToNullableNumber = Map({'a': 1, 'b': null});
// $ExpectError
stringToNumber = stringToNullableNumber;
// Specific type for filter(Boolean) which removes nullability.
stringToNumber = stringToNullableNumber.filter(Boolean)

/* OrderedMap */

orderedStringToNumber = Map({'a': 1}).toOrderedMap()
Expand Down
0