Skip to content

Commit cf6a1f1

Browse files
committed
filter over Map
For ramda/ramda#3507
1 parent 13d36d5 commit cf6a1f1

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

test/filter.test.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,3 +90,8 @@ expectType<(number | undefined)[]>(filter(x => x != null, [] as (number | undefi
9090
// filter(() => narrow, dist)
9191
// no need for type annotations when using full signature
9292
expectType<Record<string, string>>(filter(isNotNil, {} as Dict));
93+
94+
95+
// filter(() => narrow)(dist)
96+
expectType<Map<string, string>>(filter(isNotNil, new Map<string, string | undefined>()));
97+
expectType<Map<string, number>>(filter(gt5, new Map<string, number>()));

types/filter.d.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,18 @@ export function filter<A, P extends A>(
1010
<B extends A>(list: readonly B[]): P[];
1111
};
1212

13-
// filter(() => boolean)
13+
// filter(() => boolean)(list | dict)
1414
export function filter<T>(
1515
pred: (value: T) => boolean,
16-
): <P extends T, C extends readonly P[] | Record<string, P>>(collection: C) => C;
16+
): {
17+
<K>(map: Map<K, T>): Map<K, T>;
18+
<P extends T, C extends readonly P[] | Record<string, P>>(collection: C): C;
19+
};
20+
21+
// filter(() => narrow, map)
22+
export function filter<K, T, P extends T>(pred: (val: T) => val is P, map: Map<K, T>): Map<K, P>;
23+
// filter(() => boolean, map)
24+
export function filter<K, T>(pred: (val: T) => boolean, map: Map<K, T>): Map<K, T>;
1725

1826
// filter(() => narrow, list) - readonly T[] falls into Record<string T> for some reason, so list needs to come first
1927
export function filter<T, P extends T>(pred: (val: T) => val is P, list: readonly T[]): P[];

0 commit comments

Comments
 (0)