Skip to content

Commit

Permalink
feat(effects): createEffect returns specific type for dispatch false (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-okrushko authored and brandonroberts committed Nov 6, 2019
1 parent 2bac73c commit f70600f
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 9 deletions.
2 changes: 1 addition & 1 deletion modules/effects/spec/types/effect_creator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ describe('createEffect()', () => {
expectSnippet(`
const effect = createEffect(() => ({ foo: 'a' }), { dispatch: false });
`).toFail(
/Type '{ foo: string; }' is not assignable to type 'Observable<unknown> | ((...args: any[]) => Observable<unknown>)'./
/Type '{ foo: string; }' is not assignable to type 'Observable<{}> | ((...args: any[]) => Observable<{}>)'./
);
});
});
Expand Down
12 changes: 5 additions & 7 deletions modules/effects/src/effect_creator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,8 @@ interface CreateEffectMetadata {
[CREATE_EFFECT_METADATA_KEY]: EffectConfig;
}

type DispatchType<T> = T extends { dispatch: infer U } ? U : unknown;
type ObservableReturnType<T> = T extends false
? Observable<unknown>
: Observable<Action>;
type DispatchType<T> = T extends { dispatch: infer U } ? U : true;
type ObservableType<T, OriginalType> = T extends false ? OriginalType : Action;
/**
* @description
* Creates an effect from an `Observable` and an `EffectConfig`.
Expand Down Expand Up @@ -46,9 +44,9 @@ type ObservableReturnType<T> = T extends false
*/
export function createEffect<
C extends EffectConfig,
T extends DispatchType<C>,
O extends ObservableReturnType<T>,
R extends O | ((...args: any[]) => O)
DT extends DispatchType<C>,
OT extends ObservableType<DT, OT>,
R extends Observable<OT> | ((...args: any[]) => Observable<OT>)
>(source: () => R, config?: Partial<C>): R & CreateEffectMetadata {
const effect = source();
const value: EffectConfig = {
Expand Down
2 changes: 1 addition & 1 deletion modules/effects/src/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export interface EffectConfig {
resubscribeOnError?: boolean;
}

export const DEFAULT_EFFECT_CONFIG: Required<EffectConfig> = {
export const DEFAULT_EFFECT_CONFIG: Readonly<Required<EffectConfig>> = {
dispatch: true,
resubscribeOnError: true,
};
Expand Down

0 comments on commit f70600f

Please sign in to comment.