-
-
Notifications
You must be signed in to change notification settings - Fork 15.3k
/
store.ts
214 lines (173 loc) · 4.8 KB
/
store.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
import { combineReducers, createStore } from 'redux'
import type {
Store,
Reducer,
Action,
StoreEnhancer,
Unsubscribe,
Observer
} from 'redux'
import 'symbol-observable'
type BrandedString = string & { _brand: 'type' }
const brandedString = 'a string' as BrandedString
type State = {
a: 'a'
b: {
c: 'c'
d: 'd'
}
e: BrandedString
}
interface DerivedAction extends Action {
type: 'a'
b: 'b'
}
const reducer: Reducer<State> = (
state: State | undefined = {
a: 'a',
b: {
c: 'c',
d: 'd'
},
e: brandedString
},
action: Action
): State => {
return state
}
const reducerWithAction: Reducer<State, DerivedAction> = (
state: State | undefined = {
a: 'a',
b: {
c: 'c',
d: 'd'
},
e: brandedString
},
action: DerivedAction
): State => {
return state
}
const funcWithStore = (store: Store<State, DerivedAction>) => {}
/* createStore */
const store: Store<State> = createStore(reducer)
// test that nullable state is preserved
const nullableStore = createStore((): string | null => null)
expectTypeOf(nullableStore.getState()).toEqualTypeOf<string | null>()
// ensure that an array-based state works
const arrayReducer = (state: any[] = []) => state || []
const storeWithArrayState: Store<any[]> = createStore(arrayReducer)
const storeWithPreloadedState: Store<State> = createStore(reducer, {
a: 'a',
b: { c: 'c', d: 'd' },
e: brandedString
})
const storeWithBadPreloadedState: Store<State> =
// @ts-expect-error
// prettier-ignore
createStore(reducer, { b: { c: 'c' }, e: brandedString })
const reducerA: Reducer<string> = (state = 'a') => state
const reducerB: Reducer<{ c: string; d: string }> = (
state = { c: 'c', d: 'd' }
) => state
const reducerE: Reducer<BrandedString> = (state = brandedString) => state
const combinedReducer = combineReducers({
a: reducerA,
b: reducerB,
e: reducerE
})
const storeWithCombineReducer = createStore(combinedReducer, {
b: { c: 'c', d: 'd' },
e: brandedString
})
const storeWithCombineReducerAndBadPreloadedState =
// @ts-expect-error
// prettier-ignore
createStore( combinedReducer, { b: { c: 'c' }, e: brandedString } )
const nestedCombinedReducer = combineReducers({
a: (state: string = 'a') => state,
b: combineReducers({
c: (state: string = 'c') => state,
d: (state: string = 'd') => state
}),
e: (state: BrandedString = brandedString) => state
})
// @ts-expect-error
const storeWithNestedCombineReducer = createStore(nestedCombinedReducer, {
b: { c: 5 },
e: brandedString
})
const simpleCombinedReducer = combineReducers({
c: (state: string = 'c') => state,
d: (state: string = 'd') => state
})
const storeWithSimpleCombinedReducer =
// @ts-expect-error
// prettier-ignore
createStore(simpleCombinedReducer, { c: 5 })
// Note: It's not necessary that the errors occur on the lines specified, just as long as something errors somewhere
// since the preloaded state doesn't match the reducer type.
const simpleCombinedReducerWithImplicitState = combineReducers({
c: (state = 'c') => state,
d: (state = 'd') => state
})
const storeWithSimpleCombinedReducerWithImplicitState =
// @ts-expect-error
// prettier-ignore
createStore( simpleCombinedReducerWithImplicitState, { c: 5 } )
const storeWithActionReducer = createStore(reducerWithAction)
const storeWithActionReducerAndPreloadedState = createStore(reducerWithAction, {
a: 'a',
b: { c: 'c', d: 'd' },
e: brandedString
})
funcWithStore(storeWithActionReducer)
funcWithStore(storeWithActionReducerAndPreloadedState)
const storeWithActionReducerAndBadPreloadedState =
// @ts-expect-error
// prettier-ignore
createStore( reducerWithAction, { b: { c: 'c' }, e: brandedString } )
const enhancer: StoreEnhancer = next => next
const storeWithSpecificEnhancer: Store<State> = createStore(reducer, enhancer)
const storeWithPreloadedStateAndEnhancer: Store<State> = createStore(
reducer,
{
a: 'a',
b: { c: 'c', d: 'd' },
e: brandedString
},
enhancer
)
const storeWithBadPreloadedStateAndEnhancer: Store<State> = createStore(
reducer,
{
// @ts-expect-error
b: { c: 'c' }
},
enhancer
)
/* dispatch */
store.dispatch({
type: 'ADD_TODO',
text: 'test'
})
/* getState */
const state: State = store.getState()
/* subscribe / unsubscribe */
const unsubscribe: Unsubscribe = store.subscribe(() => {
console.log('Current state:', store.getState())
})
unsubscribe()
/* replaceReducer */
const newReducer: Reducer<State> = reducer
store.replaceReducer(newReducer)
/* observable */
let observable = store[Symbol.observable]()
observable = observable[Symbol.observable]()
const observer: Observer<State> = {
next(state: State) {
console.log('current state:', state)
}
}
const unsubscribeFromObservable = observable.subscribe(observer).unsubscribe
unsubscribeFromObservable()