Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
fix(typings): More correct Epic and ofType type refinement for TypeSc…
…ript users (#392) (#396)
  • Loading branch information
moajo authored and jayphelps committed Jan 10, 2018
1 parent 6bad934 commit 63b2acc
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 12 deletions.
17 changes: 8 additions & 9 deletions index.d.ts
Expand Up @@ -25,16 +25,15 @@ export declare class ActionsObservable<T extends Action> extends Observable<T> {
constructor(input$: Observable<T>);
lift<R extends Action>(operator: Operator<T, R>): ActionsObservable<R>;
lift<R>(operator: Operator<T, R>): Observable<R>;
ofType(...key: T['type'][]): ActionsObservable<T>;
ofType<R extends Action = T>(...key: R['type'][]): ActionsObservable<R>;
ofType<R extends T = T>(...key: R['type'][]): ActionsObservable<R>;
}

export declare interface Epic<T extends Action, S, D = any> {
(action$: ActionsObservable<T>, store: MiddlewareAPI<S>, dependencies: D): Observable<T>;
export declare interface Epic<T extends Action, S, D = any, O extends T = T> {
(action$: ActionsObservable<T>, store: MiddlewareAPI<S>, dependencies: D): Observable<O>;
}

export interface EpicMiddleware<T extends Action, S, D = any> extends Middleware {
replaceEpic(nextEpic: Epic<T, S, D>): void;
export interface EpicMiddleware<T extends Action, S, D = any, O extends T = T> extends Middleware {
replaceEpic(nextEpic: Epic<T, S, D, O>): void;
}

interface Adapter {
Expand All @@ -47,11 +46,11 @@ interface Options<D = any> {
dependencies?: D;
}

export declare function createEpicMiddleware<T extends Action, S, D = any>(rootEpic: Epic<T, S, D>, options?: Options<D>): EpicMiddleware<T, S, D>;
export declare function createEpicMiddleware<T extends Action, S, D = any, O extends T = T>(rootEpic: Epic<T, S, D, O>, options?: Options<D>): EpicMiddleware<T, S, D, O>;

export declare function combineEpics<T extends Action, S, D = any>(...epics: Epic<T, S, D>[]): Epic<T, S, D>;
export declare function combineEpics<T extends Action, S, D = any, O extends T = T>(...epics: Epic<T, S, D, O>[]): Epic<T, S, D, O>;
export declare function combineEpics<E>(...epics: E[]): E;

export declare function ofType<T extends Action>(...keys: T['type'][]): (source: Observable<T>) => Observable<T>;
export declare function ofType<T extends Action, R extends T = T>(...keys: T['type'][]): (source: Observable<T>) => Observable<R>;

export declare const EPIC_END: '@@redux-observable/EPIC_END';
35 changes: 32 additions & 3 deletions test/typings.ts
Expand Up @@ -80,8 +80,33 @@ const epic8: Epic<FluxStandardAction, State, Dependencies> = (action$, store, de
}))
)

const rootEpic1: Epic<FluxStandardAction, State> = combineEpics<FluxStandardAction, State>(epic1, epic2, epic3, epic4, epic5, epic6, epic7, epic8);
const rootEpic2 = combineEpics(epic1, epic2, epic3, epic4, epic5, epic6, epic7, epic8);
interface Epic9_Input {
type: "NINTH",
payload: string,
}
interface Epic9_Output {
type: "ninth",
payload: string,
}

const epic9_1: Epic<FluxStandardAction, State, Dependencies, Epic9_Output> = (action$, store, dependencies) =>
action$.pipe(
ofType<FluxStandardAction, Epic9_Input>('NINTH'),
map(({ type, payload }) => ({
type: 'ninth' as 'ninth',
payload: dependencies.func("ninth-" + payload),
}))
);
const epic9_2 = (action$: ActionsObservable<FluxStandardAction>, store: MiddlewareAPI<State>, dependencies: Dependencies) =>
action$.pipe(
ofType<FluxStandardAction, Epic9_Input>('NINTH'),
map(({ type, payload }) => ({
type: 'ninth',
payload: dependencies.func("ninth-" + payload),
} as Epic9_Output))
);
const rootEpic1: Epic<FluxStandardAction, State> = combineEpics<FluxStandardAction, State>(epic1, epic2, epic3, epic4, epic5, epic6, epic7, epic8, epic9_1);
const rootEpic2 = combineEpics(epic1, epic2, epic3, epic4, epic5, epic6, epic7, epic8, epic9_2);

const dependencies: Dependencies = {
func(value: string) { return `func-${value}`}
Expand Down Expand Up @@ -129,6 +154,7 @@ store.dispatch({ type: 'FIFTH', payload: 'fifth-payload' });
store.dispatch({ type: 'SIXTH', payload: 'sixth-payload' });
store.dispatch({ type: 'SEVENTH', payload: 'seventh-payload' });
store.dispatch({ type: 'EIGHTH', payload: 'eighth-payload' });
store.dispatch({ type: 'NINTH', payload: 'ninth-payload' });

expect(store.getState()).to.deep.equal([
{ "type": "@@redux/INIT" },
Expand Down Expand Up @@ -171,7 +197,10 @@ expect(store.getState()).to.deep.equal([
{ "type": "seventh", "payload": "func-seventh-payload" },
{ "type": "EIGHTH", "payload": "eighth-payload" },
{ "type": "eighth", "payload": "eighth-payload" },
{ "type": "eighth", "payload": "eighth-payload" }
{ "type": "eighth", "payload": "eighth-payload" },
{ "type": "NINTH", "payload": "ninth-payload" },
{ "type": "ninth", "payload": "func-ninth-ninth-payload" },
{ "type": "ninth", "payload": "func-ninth-ninth-payload" },
]);

const input$ = Observable.create(() => {});
Expand Down

0 comments on commit 63b2acc

Please sign in to comment.