/ shared / types / sagaHelpers.d.ts
sagaHelpers.d.ts
 1  import { Effect } from 'redux-saga/effects';
 2  
 3  type ExtPromise<T> = T extends Promise<infer U> ? U : T;
 4  
 5  type ExtSaga<T> = T extends IterableIterator<infer U> ? Exclude<U, Effect | Effect[]> : T;
 6  
 7  /**
 8   * Use this helper to unwrap return types from effects like Call / Apply
 9   * In the case of calling a function that returns a promise, this helper will unwrap the
10   * promise and return the type inside it. In the case of calling another saga / generator,
11   * this helper will return the actual return value of the saga / generator, otherwise,
12   * it'll return the original type.
13   *
14   * NOTE 1: When using this to extract the type of a Saga, make sure to remove the `SagaIterator`
15   * return type of the saga if it contains one, since that masks the actual return type of the saga.
16   *
17   * NOTE 2: You will most likely need to use the `typeof` operator to use this helper.
18   * E.g type X = UnwrapEffects<typeof MyFunc/MySaga>
19   */
20  export type UnwrapEffects<T> = T extends (...args: any[]) => any
21    ? ExtSaga<ReturnType<T>>
22    : ExtPromise<T>;