Skip to content

Instantly share code, notes, and snippets.

@flushentitypacket
Created January 20, 2018 19:36
Show Gist options
  • Save flushentitypacket/33003b8c78fdc133ae3de0b167f55738 to your computer and use it in GitHub Desktop.
Save flushentitypacket/33003b8c78fdc133ae3de0b167f55738 to your computer and use it in GitHub Desktop.
Typescript Redux todo feature
// 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