iterare icon indicating copy to clipboard operation
iterare copied to clipboard

[bug][filter] type definition

Open hyf0 opened this issue 4 years ago • 4 comments

for https://github.com/felixfbecker/iterare/blob/master/src/iterate.ts

now:

filter(predicate: (element: T) => boolean): IteratorWithOperators<T>
filter<R extends T>(predicate: (element: T) => element is R): IteratorWithOperators<R>
filter(predicate: (element: T) => boolean): IteratorWithOperators<T> {
    return new IteratorWithOperators(new FilterIterator(this.source, predicate))
}

expected:

filter<R extends T>(predicate: (element: T) => element is R): IteratorWithOperators<R>
filter(predicate: (element: T) => boolean): IteratorWithOperators<T>
filter(predicate: (element: T) => boolean): IteratorWithOperators<T> {
    return new IteratorWithOperators(new FilterIterator(this.source, predicate))
}

the problem is when write codes like this, you may get a wrong type

const foo = [1, 2, 3, null, undefined, 4] // (number | undefined | null)[]
const bar = iterate(foo)
  .filter((item): item is NonNullable<typeof item> => item != null)
  .toArray()

// now
bar // => (number | undefined | null)[]
// expectecd
bar // => number[]

hyf0 avatar Mar 09 '21 09:03 hyf0

So the issue is just the order of overloads?

felixfbecker avatar Mar 09 '21 09:03 felixfbecker

So the issue is just the order of overloads?

yes, i guess () => xx is XX is subset of () => boolean , so The next one will shadow the previous one

hyf0 avatar Mar 09 '21 10:03 hyf0

So the issue is just the order of overloads?

how is it going? I'd like to send a PR to fix this.but I got a problem when writing test image this code won't fail tests, which is meaningless.

I found to testing types which require other tools like tsd.I also would like to import this pkg for testing types, but I like to konw how you solve it?

hyf0 avatar Apr 09 '21 16:04 hyf0

https://github.com/dsherret/conditional-type-checks

lazarljubenovic avatar May 09 '21 04:05 lazarljubenovic