mirror of
https://github.com/microsoft/frontend-bootcamp.git
synced 2026-01-26 14:56:42 +08:00
reduced typings some more
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import { action } from '../redux-utils/action';
|
||||
import { action, GenericActionTypes, GenericAction, GenericActionLookup, GenericActionMapping } from '../redux-utils/action';
|
||||
|
||||
export const actions = {
|
||||
add: (label: string) => action('add', { label }),
|
||||
@@ -9,6 +9,7 @@ export const actions = {
|
||||
filter: (filterTypes: string) => action('filter', { filter: filterTypes })
|
||||
};
|
||||
|
||||
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]> };
|
||||
export type ActionMap = GenericActionMapping<typeof actions>;
|
||||
export type ActionTypes = GenericActionTypes<ActionMap>;
|
||||
export type TodoAction = GenericAction<ActionMap>;
|
||||
export type TodoActionLookup = GenericActionLookup<ActionMap>;
|
||||
|
||||
@@ -1,9 +1,10 @@
|
||||
import { ActionTypes, TodoActionLookup } from '../actions';
|
||||
import { ActionTypes, TodoActionLookup, actions } from '../actions';
|
||||
import { createGenericReducer, HandlerMap, ImmerReducer } from '../redux-utils/reducer';
|
||||
import { Reducer } from 'redux';
|
||||
|
||||
export function createReducer<T, AT extends ActionTypes | never = never>(
|
||||
export function createReducer<T, AM extends ActionTypes | never = never>(
|
||||
initialState: T,
|
||||
handlerOrMap: HandlerMap<T, ActionTypes, TodoActionLookup> | ImmerReducer<T, TodoActionLookup[AT]>
|
||||
) {
|
||||
return createGenericReducer(initialState, handlerOrMap);
|
||||
handlerOrMap: HandlerMap<T, typeof actions> | ImmerReducer<T, TodoActionLookup[AM]>
|
||||
): Reducer<T> {
|
||||
return createGenericReducer<T, typeof actions, AM>(initialState, handlerOrMap);
|
||||
}
|
||||
|
||||
@@ -7,3 +7,8 @@ export function action<T extends string, P>(type: T, payload: P): ActionWithPayl
|
||||
export function action<T extends string, P>(type: T, payload?: P) {
|
||||
return { type, ...payload };
|
||||
}
|
||||
|
||||
export type GenericActionMapping<A> = { [somekey in keyof A]: (...args: any) => Action<any> | ActionWithPayload<any, any> };
|
||||
export type GenericActionTypes<A extends GenericActionMapping<A>> = ReturnType<A[keyof A]>['type'];
|
||||
export type GenericAction<A extends GenericActionMapping<A>> = ReturnType<A[GenericActionTypes<A>]>;
|
||||
export type GenericActionLookup<A extends GenericActionMapping<A>> = { [a in GenericActionTypes<A>]: ReturnType<A[a]> };
|
||||
|
||||
@@ -1,13 +1,14 @@
|
||||
import { Reducer } from 'redux';
|
||||
import { Draft, produce } from 'immer';
|
||||
import { GenericActionLookup, GenericActionMapping } from './action';
|
||||
|
||||
export type ImmerReducer<T, A> = (state: Draft<T>, action?: A) => T;
|
||||
export type HandlerMap<T, AT extends string, Lookup extends { [t in AT]: any }> = {
|
||||
[actionType in AT]?: ImmerReducer<T, Lookup[actionType]>
|
||||
export type HandlerMap<T, A extends GenericActionMapping<A>> = {
|
||||
[actionType in keyof A]?: ImmerReducer<T, GenericActionLookup<A>[actionType]>
|
||||
};
|
||||
|
||||
function isHandlerFunction<T, A, AT extends string, Lookup extends { [t in AT]: any }>(
|
||||
handlerOrMap: HandlerMap<T, AT, Lookup> | ImmerReducer<T, A>
|
||||
function isHandlerFunction<T, A extends GenericActionMapping<A>>(
|
||||
handlerOrMap: HandlerMap<T, A> | ImmerReducer<T, A>
|
||||
): handlerOrMap is ImmerReducer<T, A> {
|
||||
if (typeof handlerOrMap === 'function') {
|
||||
return true;
|
||||
@@ -16,15 +17,15 @@ function isHandlerFunction<T, A, AT extends string, Lookup extends { [t in AT]:
|
||||
return false;
|
||||
}
|
||||
|
||||
export function createGenericReducer<T, AT extends string, Lookup extends { [t in AT]: any }>(
|
||||
export function createGenericReducer<T, A extends GenericActionMapping<A>, AM = keyof GenericActionMapping<A>>(
|
||||
initialState: T,
|
||||
handlerOrMap: HandlerMap<T, AT, Lookup> | ImmerReducer<T, Lookup[AT]>
|
||||
handlerOrMap: HandlerMap<T, A> | ImmerReducer<T, GenericActionLookup<A>[AM]>
|
||||
): Reducer<T> {
|
||||
return function reducer(state = initialState, action: Lookup[AT]): T {
|
||||
return function reducer(state = initialState, action: GenericActionLookup<A>[AM]): T {
|
||||
if (isHandlerFunction(handlerOrMap)) {
|
||||
return produce(state, draft => handlerOrMap(draft, action as Lookup[AT]));
|
||||
return produce(state, draft => handlerOrMap(draft, action as GenericActionLookup<A>[AM]));
|
||||
} else if (handlerOrMap.hasOwnProperty(action.type)) {
|
||||
const handler = (handlerOrMap as any)[action.type] as ImmerReducer<T, Lookup[AT]>;
|
||||
const handler = (handlerOrMap as any)[action.type] as ImmerReducer<T, GenericActionLookup<A>[AM]>;
|
||||
return produce(state, draft => handler(draft, action));
|
||||
} else {
|
||||
return state;
|
||||
|
||||
Reference in New Issue
Block a user