Skip to content

Instantly share code, notes, and snippets.

@zerobias
Created January 8, 2018 23:01
Show Gist options
  • Save zerobias/8c6aa2f286efe6fd0418c61b51786c2e to your computer and use it in GitHub Desktop.
Save zerobias/8c6aa2f286efe6fd0418c61b51786c2e to your computer and use it in GitHub Desktop.
Flow redux-act typings for making redux stores, entirely typed by action's payloads
declare module 'redux-act' {
/**
* Single "action" as it referenced from classic redux
* @typedef {{ type: string, payload: P }} Act.<P>
* @template P
*/
declare export
type Act</*::+*/P = mixed> = {
/*::+*/type: string,
/*::+*/payload: P,
error?: boolean,
}
/**
* Function "action-creator", as it referenced from classic redux
*
* For us action-creator is nonsense; it *is* an action
*
* You have to use this as action type itself
* @typedef {{(data: P) => Act}} Action.<P>
* @template P
* @export
*/
declare export
class Action<P = mixed> {
(data: P): Act<P>,
getType(): string,
}
declare export
class AsyncAction<P = mixed, D = void, E = mixed> extends Action<P> {
done: Action<{ params: P, result: D }>,
fail: Action<{ params: P, error: E }>,
}
declare export
function createAction<P>(description: string): Action<P>
/**
* Reducer, which represents typed state value
*
* Generic type S could be inferred from your
* state reduce functions
*
* @class ReduxField
* @template S
* @export
*/
declare export
class ReduxField<S> {
(state: S, action: any): S,
on<P, A/*::: Action<P> | $ReadOnlyArray<Action<P>>*/>(
actions: A,
reducer: (state: S, payload: P) => S
): ReduxField<S>,
}
declare export
function createReducer<S>(
untypedHandlers: {},
defaults: S
): ReduxField<S>
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment