diff --git a/README.md b/README.md index 964af82..aa8e1a9 100644 --- a/README.md +++ b/README.md @@ -1106,11 +1106,10 @@ export default combineReducers({ return [...state, action.payload]; case TOGGLE: - return state.map( - item => - item.id === action.payload - ? { ...item, completed: !item.completed } - : item + return state.map(item => + item.id === action.payload + ? { ...item, completed: !item.completed } + : item ); default: diff --git a/playground/package-lock.json b/playground/package-lock.json index 609a2fa..c528fd1 100644 --- a/playground/package-lock.json +++ b/playground/package-lock.json @@ -14236,9 +14236,9 @@ "integrity": "sha512-NLpRc/FY+jPfWL0aUXQzjxPyF0Xug2om6akaoRLQ18KGwP2yYNBJu9vkv2q1q+Cx/+edy2Qf6O8xXnYY/xwz1A==" }, "typescript": { - "version": "3.1.6", - "resolved": "https://registry.npmjs.org/typescript/-/typescript-3.1.6.tgz", - "integrity": "sha512-tDMYfVtvpb96msS1lDX9MEdHrW4yOuZ4Kdc4Him9oU796XldPYF/t2+uKoX0BBa0hXXwDlqYQbXY5Rzjzc5hBA==", + "version": "3.0.3", + "resolved": "https://registry.npmjs.org/typescript/-/typescript-3.0.3.tgz", + "integrity": "sha512-kk80vLW9iGtjMnIv11qyxLqZm20UklzuR2tL0QAnDIygIUIemcZMxlMWudl9OOt76H3ntVzcTiddQ1/pAAJMYg==", "dev": true }, "uglify-js": { diff --git a/playground/package.json b/playground/package.json index b7e838e..b42dc31 100644 --- a/playground/package.json +++ b/playground/package.json @@ -56,7 +56,7 @@ "ts-jest": "22.4.6", "tslint": "5.12.1", "tslint-react": "3.6.0", - "typescript": "3.1.6", + "typescript": "3.0.3", "webpack": "3.11.0", "webpack-blocks": "1.0.0" } diff --git a/playground/src/features/todos/reducer.ts b/playground/src/features/todos/reducer.ts index 4282be1..47f720c 100644 --- a/playground/src/features/todos/reducer.ts +++ b/playground/src/features/todos/reducer.ts @@ -19,11 +19,10 @@ export default combineReducers({ return [...state, action.payload]; case TOGGLE: - return state.map( - item => - item.id === action.payload - ? { ...item, completed: !item.completed } - : item + return state.map(item => + item.id === action.payload + ? { ...item, completed: !item.completed } + : item ); default: diff --git a/playground/src/store/create-reducer.ts b/playground/src/store/create-reducer.ts new file mode 100644 index 0000000..78f1e78 --- /dev/null +++ b/playground/src/store/create-reducer.ts @@ -0,0 +1,36 @@ +import { ActionType, getType } from 'typesafe-actions'; + +import { Todo } from '../features/todos/models'; +import * as actions from '../features/todos/actions'; + +export type TodosAction = ActionType; + +function createReducer( + initialState: S, + handlers: { + [P in A['type']]?: A extends { type: P } + ? (state: S, action: A) => S + : never + } +) { + return (state: S = initialState, action: A): S => { + if (handlers.hasOwnProperty(action.type)) { + return (handlers as any)[action.type](state, action); + } else { + return state; + } + }; +} + +export const todos = createReducer([], { + ['todos/ADD' as 'todos/ADD']: (state, action) => { + return [...state, action.payload]; + }, + ['todos/TOGGLE']: (state, action) => { + return state.map(item => + item.id === action.payload + ? { ...item, completed: !item.completed } + : item + ); + }, +});