Skip to content
Permalink
Browse files

fix(typings): Add custom epic overload to combineEpics (#197)

TypeScript users: you can now provide your own Epic function interface as the only generic param to `combineEpics<CustomEpic>()`. This is primarily useful when you use dependency injection:

```js
interface CustomEpic {
  (action$: ActionsObservable<Action>, store: MiddlewareAPI<State>, api: SomeApi): Observable<Action>;
}

const epic1: CustomEpic = (action$, store, api: SomeApi) =>
  action$.ofType('STUFF').etc();

const rootEpic: Epic<Action, State> = (action$, store) =>
  combineEpics<CustomEpic>(epic1)(action$, store, api);
```
  • Loading branch information...
inuscript authored and jayphelps committed Feb 20, 2017
1 parent c4318ec commit 88c0bf9e9bd44feeed1af18ec7e5639890400e72
Showing with 23 additions and 2 deletions.
  1. +2 −1 index.d.ts
  2. +21 −1 test/typings.ts
@@ -35,7 +35,7 @@ export interface EpicMiddleware<T, S> extends Middleware {
replaceEpic(nextEpic: Epic<T, S>): void;
}

interface Adapter {
interface Adapter {
input: (input$: Observable<any>) => any;
output: (output$: any) => Observable<any>;
}
@@ -47,3 +47,4 @@ interface Options {
export declare function createEpicMiddleware<T, S>(rootEpic: Epic<T, S>, options?: Options): EpicMiddleware<T, S>;

export declare function combineEpics<T, S>(...epics: Epic<T, S>[]): Epic<T, S>;
export declare function combineEpics<E>(...epics: E[]): E;
@@ -1,5 +1,5 @@
import { expect } from 'chai';
import { createStore, applyMiddleware } from 'redux';
import { createStore, applyMiddleware, MiddlewareAPI } from 'redux';
import { Observable } from 'rxjs/Observable';
import { asap } from 'rxjs/scheduler/asap';
import 'rxjs/add/observable/of';
@@ -60,6 +60,26 @@ const rootEpic2 = combineEpics(epic1, epic2, epic3, epic4, epic5, epic6);
const epicMiddleware1: EpicMiddleware<FluxStandardAction, any> = createEpicMiddleware<FluxStandardAction, any>(rootEpic1);
const epicMiddleware2 = createEpicMiddleware(rootEpic2);

interface CustomEpic<T, S, U> {
(action$: ActionsObservable<T>, store: MiddlewareAPI<S>, api: U): Observable<T>;
}

const customEpic: CustomEpic<FluxStandardAction, any, number> = (action$, store, some) =>
action$.ofType('CUSTOM1')
.map(({ type, payload }) => ({
type: 'custom1',
payload
}));

const customEpic2 = (action$, store, some) =>
action$.ofType('CUSTOM2')
.map(({ type, payload }) => ({
type: 'custom2',
payload
}));

const combinedCustomEpics = combineEpics<CustomEpic<FluxStandardAction, any, any>>(customEpic, customEpic2);

const reducer = (state = [], action) => state.concat(action);
const store = createStore(
reducer,

0 comments on commit 88c0bf9

Please sign in to comment.
You can’t perform that action at this time.