Skip to content

Commit 9e30131

Browse files
committed
feat@fix filter
1 parent abe9779 commit 9e30131

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

files/index.d.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@ MergeTypes<
8888

8989
type StrictNonNullable<T> = Exclude<T, null | undefined>;
9090

91+
type ExcludeFalsy<T> = Exclude<T, null | undefined | false | true | 0 | "">;
92+
9193
type Flatten<T> = T extends object
9294
? T extends readonly any[]
9395
? T
@@ -479,10 +481,10 @@ export function filter<T, S extends T>(
479481
): (list: T[]) => S[];
480482
export function filter<T>(
481483
predicate: BooleanConstructor,
482-
): (list: readonly T[]) => StrictNonNullable<T>[];
484+
): (list: readonly T[]) => ExcludeFalsy<T>[];
483485
export function filter<T>(
484486
predicate: BooleanConstructor,
485-
): (list: T[]) => StrictNonNullable<T>[];
487+
): (list: T[]) => ExcludeFalsy<T>[];
486488
export function filter<T>(
487489
predicate: (value: T) => boolean,
488490
): (list: T[]) => T[];

source/filter-spec.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ describe('R.filter with array', () => {
1313
)
1414
result // $ExpectType number[]
1515
})
16+
1617
it('narrowing type', () => {
1718
interface Foo {
1819
a: number
@@ -31,6 +32,7 @@ describe('R.filter with array', () => {
3132
)
3233
result // $ExpectType Bar[]
3334
})
35+
3436
it('narrowing type - readonly', () => {
3537
interface Foo {
3638
a: number
@@ -49,11 +51,19 @@ describe('R.filter with array', () => {
4951
)
5052
result // $ExpectType Bar[]
5153
})
52-
it('filtering NonNullable', () => {
53-
const testList = [1, 2, null, undefined, 3]
54+
55+
it('filtering NonNullable', () => {
56+
const testList = [1, 2, null, undefined, false, 3]
5457
const result = pipe(testList, filter(Boolean))
5558
result // $ExpectType number[]
5659
})
60+
61+
it('filtering NonNullable - list of objects', () => {
62+
const testList = [{a:1}, {a:2}, false, {a:3}]
63+
const result = pipe(testList, filter(Boolean))
64+
result // $ExpectType number[]
65+
})
66+
5767
it('filtering NonNullable - readonly', () => {
5868
const testList = [1, 2, null, undefined, 3] as const
5969
const result = pipe(testList, filter(Boolean))

0 commit comments

Comments
 (0)