Skip to content

Commit f70600f

Browse files
alex-okrushkobrandonroberts
authored andcommitted
feat(effects): createEffect returns specific type for dispatch false (#2195)
1 parent 2bac73c commit f70600f

File tree

3 files changed

+7
-9
lines changed

3 files changed

+7
-9
lines changed

modules/effects/spec/types/effect_creator.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ describe('createEffect()', () => {
4747
expectSnippet(`
4848
const effect = createEffect(() => ({ foo: 'a' }), { dispatch: false });
4949
`).toFail(
50-
/Type '{ foo: string; }' is not assignable to type 'Observable<unknown> | ((...args: any[]) => Observable<unknown>)'./
50+
/Type '{ foo: string; }' is not assignable to type 'Observable<{}> | ((...args: any[]) => Observable<{}>)'./
5151
);
5252
});
5353
});

modules/effects/src/effect_creator.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,8 @@ interface CreateEffectMetadata {
88
[CREATE_EFFECT_METADATA_KEY]: EffectConfig;
99
}
1010

11-
type DispatchType<T> = T extends { dispatch: infer U } ? U : unknown;
12-
type ObservableReturnType<T> = T extends false
13-
? Observable<unknown>
14-
: Observable<Action>;
11+
type DispatchType<T> = T extends { dispatch: infer U } ? U : true;
12+
type ObservableType<T, OriginalType> = T extends false ? OriginalType : Action;
1513
/**
1614
* @description
1715
* Creates an effect from an `Observable` and an `EffectConfig`.
@@ -46,9 +44,9 @@ type ObservableReturnType<T> = T extends false
4644
*/
4745
export function createEffect<
4846
C extends EffectConfig,
49-
T extends DispatchType<C>,
50-
O extends ObservableReturnType<T>,
51-
R extends O | ((...args: any[]) => O)
47+
DT extends DispatchType<C>,
48+
OT extends ObservableType<DT, OT>,
49+
R extends Observable<OT> | ((...args: any[]) => Observable<OT>)
5250
>(source: () => R, config?: Partial<C>): R & CreateEffectMetadata {
5351
const effect = source();
5452
const value: EffectConfig = {

modules/effects/src/models.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export interface EffectConfig {
1313
resubscribeOnError?: boolean;
1414
}
1515

16-
export const DEFAULT_EFFECT_CONFIG: Required<EffectConfig> = {
16+
export const DEFAULT_EFFECT_CONFIG: Readonly<Required<EffectConfig>> = {
1717
dispatch: true,
1818
resubscribeOnError: true,
1919
};

0 commit comments

Comments
 (0)