-
Notifications
You must be signed in to change notification settings - Fork 17
/
ts-test.ts
58 lines (44 loc) · 1.43 KB
/
ts-test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import { createReducer } from './'
interface Action {
type: string;
}
interface State {
value: number;
}
class Reset implements Action {
readonly type = 'Reset Action';
}
class AddOne implements Action {
readonly type = 'AddOne Action';
}
class AddCustom implements Action {
readonly type = 'AddCustom Action';
constructor(public readonly value: number) { }
}
type Actions = Reset | AddOne | AddCustom;
const reducer = createReducer<State, Actions>({ value: 0 }, {
'Reset Action': (state, action) => ({ value: 0 }),
'AddOne Action': (state, action) => ({ value: state.value + 1 }),
'AddCustom Action': (state, action) => ({ value: state.value + action.value }),
});
type ActionsWithoutAddOne = Exclude<Actions, AddOne>
const reducerThatDoesNotHandleAddOne = createReducer<State, ActionsWithoutAddOne>({ value: 0 }, {
'Reset Action': (state, action) => ({ value: 0 }),
'AddCustom Action': (state, action) => ({ value: state.value + action.value }),
});
// Make sure the old usage still works:
const ActionTypes = {
ADD_TODO: 'ADD TODO',
REMOVE_TODO: 'REMOVE TODO'
}
const initialState = [];
export const todos = createReducer(initialState, {
[ActionTypes.ADD_TODO](state, action) {
const text = action.text.trim();
return [...state, text];
},
[ActionTypes.REMOVE_TODO](state, action) {
return state.filter((_, i) => i !== action.index);
}
// All other action types result in state being returned
});