Improve `ofType` type declaration for TS >3.3

Author: tjfryanCreated Mar 1, 2019Updated Jul 25, 2026
Labelsreviewed

What is the current behavior?

When using redux-observable in TypeScript, providing a key or keys to ActionsObservable.prototype.ofType doesn't refine the type of the actions emitted and often requires a redundant action.type check in order to maintain type correctness.

What is the expected behavior?

With TypeScript 3.3, it is possible for the type system to automatically refine the Action union to only the options that match the provided keys. Here's a rough demo

I'm currently using the following custom typedef as a stop-gap in my codebase.

type ActionByType<Union, Type> = Union extends {type: Type} ? Union : never;

declare module 'redux-observable' {
  interface ActionsObservable<T extends Action> {
    ofType<A extends Array<Nullable<T['type']>>>(
      ...keys: A
    ): ActionsObservable<ActionByType<T, A[number]>>;
  }
}

Source: redux-observable/redux-observable