Fix imapfilter typing

This commit is contained in:
Michaël Lemaire 2021-08-26 23:50:26 +02:00
parent 43d5678697
commit 8fbb5c4ad1

11
mod.ts
View file

@ -341,17 +341,16 @@ export function ifilter<T>(
/** /**
* Apply map, and filter invalid results in the same pass. * Apply map, and filter invalid results in the same pass.
*/ */
export function imapfilter<T1, T2, E>( export function imapfilter<T1, T2>(
iterable: Iterable<T1>, iterable: Iterable<T1>,
mapfunc: (_: T1) => Exclude<T2, E> | typeof exclusion, mapfunc: (_: T1) => T2 | null,
exclusion: E = null as unknown as E, ): Iterable<Exclude<T2, null>> {
): Iterable<T2> {
return { return {
[Symbol.iterator]: function* () { [Symbol.iterator]: function* () {
for (let value of iterable) { for (let value of iterable) {
const mapped = mapfunc(value); const mapped = mapfunc(value);
if (mapped !== exclusion) { if (mapped !== null) {
yield mapped as T2; yield mapped as Exclude<T2, null>;
} }
} }
}, },