Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Observable.filter with type guard predicate #739

Open
joux3 opened this issue Jun 15, 2019 · 3 comments
Open

Observable.filter with type guard predicate #739

joux3 opened this issue Jun 15, 2019 · 3 comments

Comments

@joux3
Copy link
Contributor

joux3 commented Jun 15, 2019

Array.filter can change the type of the array when passed a user-defined type guard. See for example this:

// this gets inferred as (number | string)[]
const numberOrStringArray = [1, 2, 'jee']
// this is inferred as number[] thanks to the type guard
const numberArray = numberOrStringArray
  .filter((x): x is number => typeof x === 'number')

Could Observable.filter support a similar type guard? The type definition for Array.filter in the example is:

filter<S extends T>(callbackfn: (value: T, index: number, array: T[]) => value is S, thisArg?: any): S[];
@raimohanska
Copy link
Contributor

This is a nice idea! Want to have a stab at this yourself?

@raimohanska
Copy link
Contributor

Filter is a bit hard to modify, because it's already quite overloaded:

filter(f: Predicate<V> | boolean | Property<boolean>): this

A new operator on EventStream and Property can be added though:

  narrow<V2 extends V>(f: IsA<V, V2>): EventStream<V2> {
    return <any>filter<V>(this, f)
  }

What should we call it?

@raimohanska
Copy link
Contributor

WIP at 745de93

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants