mirror of
https://github.com/microsoft/frontend-bootcamp.git
synced 2026-01-26 14:56:42 +08:00
24 lines
1.2 KiB
TypeScript
24 lines
1.2 KiB
TypeScript
import { Action } from 'redux';
|
|
|
|
type ActionWithPayload<T, P> = Action<T> & P;
|
|
|
|
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) => 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]> };
|