Skip to content

Commit

Permalink
Merge pull request #134 from deadNightTiger/type-action-creators
Browse files Browse the repository at this point in the history
Derive types for transform/process/validate hooks
  • Loading branch information
jeffbski committed Jan 19, 2019
2 parents dcfc476 + a4390ff commit 45b7d8f
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 26 deletions.
44 changes: 22 additions & 22 deletions definitions/logic.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,12 @@ export interface CreateLogic {
Meta extends Object = undefined,
Dependency extends object = {},
Context extends Object = undefined,
Type extends string = string
Type extends string = string,
Action extends StandardAction<Type, Payload, Meta> = StandardAction<Type, Payload, Meta>,
>(
config: CreateLogic.Config<
State,
Action<Type, Payload, Meta>,
Action,
Dependency,
Context,
Type
Expand All @@ -84,11 +85,12 @@ export interface CreateLogic {
Payload extends Object = undefined,
Meta extends Object = undefined,
Dependency extends object = {},
Type extends string = string
Type extends string = string,
Action extends StandardAction<Type, Payload, Meta> = StandardAction<Type, Payload, Meta>,
>(
config: CreateLogic.Config<
State,
Action<Type, Payload, Meta>,
Action,
Dependency,
undefined,
Type
Expand All @@ -100,23 +102,25 @@ export interface CreateLogic {
State extends object,
Dependency extends object = {},
Context extends Object = undefined,
Type extends string = string
Type extends string = string,
Action extends StandardAction<Type> = StandardAction<Type>
>(
config: CreateLogic.Config<State, Action<Type>, Dependency, Context, Type>
config: CreateLogic.Config<State, Action, Dependency, Context, Type>
): Logic<State, undefined, undefined, Dependency, Context, Type>;

// createLogic with State and Type only
<State extends object, Type extends string = string>(
config: CreateLogic.Config<State, Action<Type>, {}, undefined, Type>
<State extends object, Type extends string = string, Action extends StandardAction<Type> = StandardAction<Type>>(
config: CreateLogic.Config<State, Action, {}, undefined, Type>
): Logic<State, undefined, undefined, {}, undefined, Type>;

// createLogic with State, Dependency and Type only
<
State extends object,
Dependency extends object = {},
Type extends string = string
Type extends string = string,
Action extends StandardAction<Type> = StandardAction<Type>
>(
config: CreateLogic.Config<State, Action<Type>, Dependency, undefined, Type>
config: CreateLogic.Config<State, Action, Dependency, undefined, Type>
): Logic<State, undefined, undefined, Dependency, undefined, Type>;
}

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

export type ActionCreatorType<Action extends StandardAction> = {
(payload: PayloadExtractor<Action>): Action;
toString(): string;
}

export type PrimitiveType<Type extends string | symbol, InputPayload> =
| Type
| RegExp
Expand All @@ -168,7 +177,7 @@ export namespace CreateLogic {
Type extends string
> {
name?: string | Function;
type: TypeMatcher<Type, PayloadExtractor<Action>>;
type: TypeMatcher<Type, PayloadExtractor<Action>> | ActionCreatorType<Action>;
cancelType?: TypeMatcher<string, PayloadExtractor<Action>>;
latest?: boolean;
debounce?: number;
Expand Down Expand Up @@ -253,9 +262,7 @@ export namespace CreateLogic {
Context extends Object = undefined
> {
processOptions?: Process.Options<Action>;
process?:
| Process.SimpleHook<State, Action, Dependency, Context>
| Process.AdvancedHook<State, Action, Dependency, Context>;
process?: Process.Hook<State, Action, Dependency, Context>;
}

export namespace Process {
Expand All @@ -276,14 +283,7 @@ export namespace CreateLogic {
ctx: Context;
};

export type SimpleHook<
State extends object,
Action extends StandardAction,
Dependency extends object,
Context extends Object = undefined
> = (depObj: Process.DepObj<State, Action, Dependency, Context>) => void;

export type AdvancedHook<
export type Hook<
State extends object,
Action extends StandardAction,
Dependency extends object,
Expand Down
33 changes: 30 additions & 3 deletions test/typecheck.createLogic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ import { Dependency, Meta, Payload, State } from './typecheck';
dispatchReturn: true,
successType: 'successType'
},
process({ getState, action, cancelled$ }): void {
process({ getState, action, cancelled$ }: { getState: any, action: any, cancelled$: any }): void {
let state: State = getState();
let expectedAction:
| (StandardAction<'type'>)
Expand All @@ -202,7 +202,7 @@ import { Dependency, Meta, Payload, State } from './typecheck';
dispatchReturn: true,
successType: (payload: Payload) => ({ type: 'successType' })
},
process({ getState, action, cancelled$ }) {
process({ getState, action, cancelled$ }: { getState: any, action: any, cancelled$: any }) {
let state: State = getState();
let expectedAction:
| (ActionBasis<'type'> & PayloadBasis<Payload> & MetaBasis<Meta>)
Expand All @@ -218,7 +218,7 @@ import { Dependency, Meta, Payload, State } from './typecheck';
dispatchReturn: true,
dispatchMultiple: false
},
process(depObj) {
process(depObj: any) {
let dep: Dependency = depObj;
let state: State = depObj.getState();
let expectedAction: (ActionBasis<'type'>) | ErroneousAction<'type'> =
Expand Down Expand Up @@ -280,6 +280,33 @@ import { Dependency, Meta, Payload, State } from './typecheck';
});
}

{
interface ActionCreator<P> {
(payload: P): { type: string; payload: P };
toString(): string;
}
let fooAction: ActionCreator<{ foo: number }>;
const logic1 = createLogic({
type: fooAction,
validate({action}) {
action.payload.foo.toFixed();
},
});
const logic2 = createLogic({
type: fooAction,
process({ action }) {
action.payload.foo.toExponential();
},
});
const logic3 = createLogic({
type: fooAction,
process({ action }, dispatch, done) {
action.payload.foo.toExponential();
},
})
}


//
// EXPECT ERROR
//
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"moduleResolution": "node",
"esModuleInterop": true,
"noEmit": true,
"noImplicitAny": false,
"noImplicitAny": true,
"noImplicitUseStrict": false,
"noLib": false,
"noResolve": false,
Expand Down

0 comments on commit 45b7d8f

Please sign in to comment.