battled and won typings

This commit is contained in:
Ken
2019-01-31 11:21:05 -08:00
parent 578ad8221d
commit fceb24f985
5 changed files with 55 additions and 31 deletions

View File

@@ -1,15 +1,23 @@
import { Action, ActionCreator } from 'redux';
import { Action } from 'redux';
export type ActionTypes = 'add' | 'remove' | 'edit' | 'complete' | 'completeAll' | 'clear' | 'filter';
type ActionWithPayload<T, P> = Action<T> & P;
export interface TodoAction extends Action<ActionTypes> {
[extraProps: string]: any;
function action<T extends string>(type: T): Action<T>;
function action<T extends string, P>(type: T, payload: P): ActionWithPayload<T, P>;
function action<T extends string, P>(type: T, payload?: P) {
return { type, ...payload };
}
export const add = (label: string): TodoAction => ({ type: 'add', label });
export const remove = (id: string): TodoAction => ({ type: 'remove', id });
export const edit = (id: string, label: string): TodoAction => ({ type: 'edit', id, label });
export const complete = (id: string): TodoAction => ({ type: 'complete', id });
export const completeAll = (): TodoAction => ({ type: 'completeAll' });
export const clear = (): TodoAction => ({ type: 'clear' });
export const filter = (filterTypes: string): TodoAction => ({ type: 'filter', filter: filterTypes });
export const add = (label: string) => action('add', { label });
export const remove = (id: string) => ({ type: 'remove' as 'remove', id });
export const edit = (id: string, label: string) => ({ type: 'edit' as 'edit', id, label });
export const complete = (id: string) => ({ type: 'complete' as 'complete', id });
export const completeAll = () => ({ type: 'completeAll' as 'completeAll' });
export const clear = () => ({ type: 'clear' as 'clear' });
export const filter = (filterTypes: string) => ({ type: 'filter' as 'filter', filter: filterTypes });
export const actions = { add, remove, edit, complete, completeAll, clear, filter };
export type ActionTypes = ReturnType<typeof actions[keyof typeof actions]>['type'];
export type TodoAction = ReturnType<typeof actions[ActionTypes]>;
export type TodoActionLookup = { [a in ActionTypes]: ReturnType<typeof actions[a]> };