Created
January 20, 2018 19:36
-
-
Save flushentitypacket/33003b8c78fdc133ae3de0b167f55738 to your computer and use it in GitHub Desktop.
Typescript Redux todo feature
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// store/todo/addTodo.ts | |
import {Reducer} from 'redux' | |
import {Action} from 'store/redux' // our fancy new generic type! | |
interface Todo { | |
id: number | |
text: string | |
} | |
const ADD = 'todo/ADD' | |
interface AddTodoActionPayload extends Todo {} | |
interface AddTodoAction extends Action<typeof ADD, AddTodoActionPayload> {} | |
let nextTodoId = 0 | |
const addTodo = (text: string): AddTodoAction => ({ | |
type: ADD, | |
payload: { | |
id: nextTodoId++, | |
text, | |
}, | |
}) | |
export const actions = { | |
addTodo, | |
} | |
export type State = Todo[] | |
const initialState: State = [] | |
export const reducer: Reducer<State> = (state: State = initialState, action: AddTodoAction): State => { | |
switch (action.type) { | |
case ADD: | |
return [ | |
...state, | |
action.payload, | |
] | |
default: | |
return state | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment