Skip to content

Commit a4390ff

Browse files
committed
Derive types for transform/process/validate hooks
When the 'type' for createLogic is an action creator, we can see what it's returning and derive the type of the action for the hooks later.
1 parent 1c322a0 commit a4390ff

File tree

2 files changed

+49
-22
lines changed

2 files changed

+49
-22
lines changed

definitions/logic.d.ts

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,12 @@ export interface CreateLogic {
6767
Meta extends Object = undefined,
6868
Dependency extends object = {},
6969
Context extends Object = undefined,
70-
Type extends string = string
70+
Type extends string = string,
71+
Action extends StandardAction<Type, Payload, Meta> = StandardAction<Type, Payload, Meta>,
7172
>(
7273
config: CreateLogic.Config<
7374
State,
74-
Action<Type, Payload, Meta>,
75+
Action,
7576
Dependency,
7677
Context,
7778
Type
@@ -84,11 +85,12 @@ export interface CreateLogic {
8485
Payload extends Object = undefined,
8586
Meta extends Object = undefined,
8687
Dependency extends object = {},
87-
Type extends string = string
88+
Type extends string = string,
89+
Action extends StandardAction<Type, Payload, Meta> = StandardAction<Type, Payload, Meta>,
8890
>(
8991
config: CreateLogic.Config<
9092
State,
91-
Action<Type, Payload, Meta>,
93+
Action,
9294
Dependency,
9395
undefined,
9496
Type
@@ -100,23 +102,25 @@ export interface CreateLogic {
100102
State extends object,
101103
Dependency extends object = {},
102104
Context extends Object = undefined,
103-
Type extends string = string
105+
Type extends string = string,
106+
Action extends StandardAction<Type> = StandardAction<Type>
104107
>(
105-
config: CreateLogic.Config<State, Action<Type>, Dependency, Context, Type>
108+
config: CreateLogic.Config<State, Action, Dependency, Context, Type>
106109
): Logic<State, undefined, undefined, Dependency, Context, Type>;
107110

108111
// createLogic with State and Type only
109-
<State extends object, Type extends string = string>(
110-
config: CreateLogic.Config<State, Action<Type>, {}, undefined, Type>
112+
<State extends object, Type extends string = string, Action extends StandardAction<Type> = StandardAction<Type>>(
113+
config: CreateLogic.Config<State, Action, {}, undefined, Type>
111114
): Logic<State, undefined, undefined, {}, undefined, Type>;
112115

113116
// createLogic with State, Dependency and Type only
114117
<
115118
State extends object,
116119
Dependency extends object = {},
117-
Type extends string = string
120+
Type extends string = string,
121+
Action extends StandardAction<Type> = StandardAction<Type>
118122
>(
119-
config: CreateLogic.Config<State, Action<Type>, Dependency, undefined, Type>
123+
config: CreateLogic.Config<State, Action, Dependency, undefined, Type>
120124
): Logic<State, undefined, undefined, Dependency, undefined, Type>;
121125
}
122126

@@ -142,6 +146,11 @@ export namespace CreateLogic {
142146
action$: Observable<Action>;
143147
};
144148

149+
export type ActionCreatorType<Action extends StandardAction> = {
150+
(payload: PayloadExtractor<Action>): Action;
151+
toString(): string;
152+
}
153+
145154
export type PrimitiveType<Type extends string | symbol, InputPayload> =
146155
| Type
147156
| RegExp
@@ -168,7 +177,7 @@ export namespace CreateLogic {
168177
Type extends string
169178
> {
170179
name?: string | Function;
171-
type: TypeMatcher<Type, PayloadExtractor<Action>>;
180+
type: TypeMatcher<Type, PayloadExtractor<Action>> | ActionCreatorType<Action>;
172181
cancelType?: TypeMatcher<string, PayloadExtractor<Action>>;
173182
latest?: boolean;
174183
debounce?: number;
@@ -253,9 +262,7 @@ export namespace CreateLogic {
253262
Context extends Object = undefined
254263
> {
255264
processOptions?: Process.Options<Action>;
256-
process?:
257-
| Process.SimpleHook<State, Action, Dependency, Context>
258-
| Process.AdvancedHook<State, Action, Dependency, Context>;
265+
process?: Process.Hook<State, Action, Dependency, Context>;
259266
}
260267

261268
export namespace Process {
@@ -276,14 +283,7 @@ export namespace CreateLogic {
276283
ctx: Context;
277284
};
278285

279-
export type SimpleHook<
280-
State extends object,
281-
Action extends StandardAction,
282-
Dependency extends object,
283-
Context extends Object = undefined
284-
> = (depObj: Process.DepObj<State, Action, Dependency, Context>) => void;
285-
286-
export type AdvancedHook<
286+
export type Hook<
287287
State extends object,
288288
Action extends StandardAction,
289289
Dependency extends object,

test/typecheck.createLogic.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,33 @@ import { Dependency, Meta, Payload, State } from './typecheck';
280280
});
281281
}
282282

283+
{
284+
interface ActionCreator<P> {
285+
(payload: P): { type: string; payload: P };
286+
toString(): string;
287+
}
288+
let fooAction: ActionCreator<{ foo: number }>;
289+
const logic1 = createLogic({
290+
type: fooAction,
291+
validate({action}) {
292+
action.payload.foo.toFixed();
293+
},
294+
});
295+
const logic2 = createLogic({
296+
type: fooAction,
297+
process({ action }) {
298+
action.payload.foo.toExponential();
299+
},
300+
});
301+
const logic3 = createLogic({
302+
type: fooAction,
303+
process({ action }, dispatch, done) {
304+
action.payload.foo.toExponential();
305+
},
306+
})
307+
}
308+
309+
283310
//
284311
// EXPECT ERROR
285312
//

0 commit comments

Comments
 (0)