mirror of
https://github.com/zhigang1992/redux.git
synced 2026-06-17 23:25:31 +08:00
Remove redundant type parameters from `Dispatch` type Remove redundant `combineReducers` overload Simplify `StoreCreator` type by using optional arguments Remove redundant type parameters from `Middleware` type Update `compose` definitions to cover case with zero arguments and case with multiple arguments for innermost function Add JSDoc
60 lines
1.1 KiB
TypeScript
60 lines
1.1 KiB
TypeScript
import {
|
|
Reducer, Action, combineReducers,
|
|
ReducersMapObject
|
|
} from "../../index.d.ts";
|
|
|
|
|
|
type TodosState = string[];
|
|
|
|
interface AddTodoAction extends Action {
|
|
text: string;
|
|
}
|
|
|
|
|
|
const todosReducer: Reducer<TodosState> = (state: TodosState,
|
|
action: Action): TodosState => {
|
|
switch (action.type) {
|
|
case 'ADD_TODO':
|
|
return [...state, (<AddTodoAction>action).text]
|
|
default:
|
|
return state
|
|
}
|
|
}
|
|
|
|
const todosState: TodosState = todosReducer([], {
|
|
type: 'ADD_TODO',
|
|
text: 'test',
|
|
});
|
|
|
|
|
|
type CounterState = number;
|
|
|
|
|
|
const counterReducer: Reducer<CounterState> = (
|
|
state: CounterState, action: Action
|
|
): CounterState => {
|
|
switch (action.type) {
|
|
case 'INCREMENT':
|
|
return state + 1
|
|
default:
|
|
return state
|
|
}
|
|
}
|
|
|
|
|
|
type RootState = {
|
|
todos: TodosState;
|
|
counter: CounterState;
|
|
}
|
|
|
|
|
|
const rootReducer: Reducer<RootState> = combineReducers<RootState>({
|
|
todos: todosReducer,
|
|
counter: counterReducer,
|
|
})
|
|
|
|
const rootState: RootState = rootReducer(undefined, {
|
|
type: 'ADD_TODO',
|
|
text: 'test',
|
|
})
|