-
Notifications
You must be signed in to change notification settings - Fork 13.2k
Closed
Labels
DuplicateAn existing issue was already createdAn existing issue was already created
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.
Metadata
Metadata
Assignees
Labels
DuplicateAn existing issue was already createdAn existing issue was already created