Skip to content

Commit

Permalink
fix: improve typings for the filter operator (#20)
Browse files Browse the repository at this point in the history
Co-authored-by: Felix Becker <[email protected]>
  • Loading branch information
lazarljubenovic and felixfbecker authored Jun 6, 2020
1 parent b30ed1c commit 3a70457
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions src/filter.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
export class FilterIterator<T> implements Iterator<T> {
constructor(private source: Iterator<T>, private predicate: (element: T) => boolean) {}
export class FilterIterator<T, V extends T = T> implements Iterator<T> {
constructor(
private source: Iterator<T>,
private predicate: ((element: T) => element is V) | ((element: T) => boolean)
) {}

next(): IteratorResult<T> {
next(): IteratorResult<V> {
let result: IteratorResult<T>
// Skip elements until predicate returns true
do {
result = this.source.next()
} while (!result.done && !this.predicate(result.value))
return result
return result as IteratorResult<V>
}
}

0 comments on commit 3a70457

Please sign in to comment.