Mirror: 🎩 A tiny but capable push & pull stream library for TypeScript and Flow
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

fix: Add missing filter overload to allow for type narrowing (#149)

authored by

Phil Pluckthun and committed by
GitHub
6111f95c e1b4c906

+16 -5
+5
.changeset/khaki-lemons-argue.md
··· 1 + --- 2 + 'wonka': patch 3 + --- 4 + 5 + Add missing overload definition for `filter`, which allows types to be narrowed, e.g. by specifying a type predicate return type.
+4 -2
src/__tests__/operators.test.ts
··· 231 231 passesAsyncSequence(noop); 232 232 233 233 it('prevents emissions for which a predicate fails', () => { 234 - const { source, next } = sources.makeSubject(); 234 + const { source, next } = sources.makeSubject<boolean>(); 235 235 const fn = vi.fn(); 236 236 237 - sinks.forEach(fn)(operators.filter(x => !!x)(source)); 237 + sinks.forEach((x: true) => { 238 + fn(x); 239 + })(operators.filter((x): x is true => !!x)(source)); 238 240 239 241 next(false); 240 242 expect(fn).not.toHaveBeenCalled();
+7 -3
src/operators.ts
··· 1 - import { Source, Sink, Operator, SignalKind, TalkbackKind, TalkbackFn } from './types'; 1 + import { Push, Source, Sink, Operator, SignalKind, TalkbackKind, TalkbackFn } from './types'; 2 2 import { push, start, talkbackPlaceholder } from './helpers'; 3 3 import { fromArray } from './sources'; 4 4 ··· 268 268 * ); 269 269 * ``` 270 270 */ 271 - export function filter<T>(predicate: (value: T) => boolean): Operator<T, T> { 271 + function filter<In, Out extends In>(predicate: (value: In) => value is Out): Operator<In, Out>; 272 + function filter<T>(predicate: (value: T) => boolean): Operator<T, T>; 273 + function filter<In, Out>(predicate: (value: In) => boolean): Operator<In, Out> { 272 274 return source => sink => { 273 275 let talkback = talkbackPlaceholder; 274 276 source(signal => { ··· 280 282 } else if (!predicate(signal[0])) { 281 283 talkback(TalkbackKind.Pull); 282 284 } else { 283 - sink(signal); 285 + sink(signal as Push<any>); 284 286 } 285 287 }); 286 288 }; 287 289 } 290 + 291 + export { filter }; 288 292 289 293 /** Maps emitted values using the passed mapping function. 290 294 *