Closed
Description
TypeScript Version: 2.4.2
It would be really nice if certain boolean-returning functions could be inferred to be type predicates. For example, in RxJS one of the signatures for the filter
method is
function filter<T, S extends T>(
this: Observable<T>,
predicate: (value: T, index: number) => value is S,
thisArg?: any
): Observable<S>;
It's very useful to filter on an observable and thereby have the resulting observable's type be refined:
type Action =
| { type: 'FOO'; fooField: number }
| { type: 'BAR'; barField: string }
action$
.filter(action => action.type === 'FOO')
.mergeMap(({ fooField }) => ...)
however this won't work because the lambda action => action.type === 'FOO'
is not inferred to have the type (action: Action) => action is { type: 'FOO'; fooField: number }
, even though the compiler should know this is true.