Skip to content

Commit 792d0b4

Browse files
authored
Merge pull request #4484 from Methuselah96/typescript-tests-strict
2 parents db753b1 + 08a38de commit 792d0b4

File tree

4 files changed

+50
-33
lines changed

4 files changed

+50
-33
lines changed

src/types/store.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,11 @@ export type Observer<T> = {
120120
* @template A the type of actions which may be dispatched by this store.
121121
* @template StateExt any extension to state from store enhancers
122122
*/
123-
export interface Store<S = any, A extends Action = AnyAction, StateExt = {}> {
123+
export interface Store<
124+
S = any,
125+
A extends Action = AnyAction,
126+
StateExt extends {} = {}
127+
> {
124128
/**
125129
* Dispatches an action. It is the only way to trigger a state change.
126130
*

test/typescript/enhancers.ts

+42-29
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@ import {
33
Action,
44
AnyAction,
55
Reducer,
6-
createStore,
7-
Store
6+
createStore
87
} from '../../src'
98

109
interface State {
@@ -157,17 +156,23 @@ function replaceReducerExtender() {
157156
}
158157
}
159158

159+
interface PartialState {
160+
someField?: 'string'
161+
test?: boolean
162+
}
163+
164+
const initialReducer: Reducer<PartialState, Action<unknown>> = () => ({
165+
someField: 'string'
166+
})
160167
const store = createStore<
161-
{ someField?: 'string'; test?: boolean },
168+
PartialState,
162169
Action<unknown>,
163170
{ method(): string },
164171
ExtraState
165-
>(reducer, enhancer)
172+
>(initialReducer, enhancer)
166173

167-
const newReducer = (
168-
state: { test: boolean } = { test: true },
169-
_: AnyAction
170-
) => state
174+
const newReducer = (state: PartialState = { test: true }, _: AnyAction) =>
175+
state
171176

172177
store.replaceReducer(newReducer)
173178
store.getState().test
@@ -234,23 +239,27 @@ function mhelmersonExample() {
234239
}
235240
}
236241

237-
const store = createStore<
238-
{ someField?: 'string'; test?: boolean },
239-
Action<unknown>,
240-
{},
241-
ExtraState
242-
>(reducer, enhancer)
243-
store.replaceReducer(reducer)
242+
interface PartialState {
243+
someField?: 'string'
244+
test?: boolean
245+
}
246+
247+
const initialReducer: Reducer<PartialState, Action<unknown>> = () => ({
248+
someField: 'string'
249+
})
250+
const store = createStore<PartialState, Action<unknown>, {}, ExtraState>(
251+
initialReducer,
252+
enhancer
253+
)
254+
store.replaceReducer(initialReducer)
244255

245256
store.getState().extraField
246257
// @ts-expect-error
247258
store.getState().wrongField
248259
store.getState().test
249260

250-
const newReducer = (
251-
state: { test: boolean } = { test: true },
252-
_: AnyAction
253-
) => state
261+
const newReducer = (state: PartialState = { test: true }, _: AnyAction) =>
262+
state
254263

255264
store.replaceReducer(newReducer)
256265
store.getState().test
@@ -304,21 +313,25 @@ function finalHelmersonExample() {
304313
}
305314
}
306315

307-
const store = createStore<
308-
{ someField?: 'string'; test?: boolean },
309-
Action<unknown>,
310-
{},
311-
ExtraState
312-
>(reducer, createPersistEnhancer('hi'))
316+
interface PartialState {
317+
someField?: 'string'
318+
test?: boolean
319+
}
320+
321+
const initialReducer: Reducer<PartialState, Action<unknown>> = () => ({
322+
someField: 'string'
323+
})
324+
const store = createStore<PartialState, Action<unknown>, {}, ExtraState>(
325+
initialReducer,
326+
createPersistEnhancer('hi')
327+
)
313328

314329
store.getState().foo
315330
// @ts-expect-error
316331
store.getState().wrongField
317332

318-
const newReducer = (
319-
state: { test: boolean } = { test: true },
320-
_: AnyAction
321-
) => state
333+
const newReducer = (state: PartialState = { test: true }, _: AnyAction) =>
334+
state
322335

323336
store.replaceReducer(newReducer)
324337
store.getState().test

test/typescript/reducers.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -150,9 +150,9 @@ function discriminated() {
150150

151151
const cs = combined(undefined, { type: 'INCREMENT' })
152152
combined(cs, { type: 'MULTIPLY' })
153-
// TODO // @ts-expect-error
153+
// @ts-expect-error
154154
combined(cs, { type: 'init' })
155-
// TODO // @ts-expect-error
155+
// @ts-expect-error
156156
combined(cs, { type: 'SOME_OTHER_TYPE' })
157157

158158
// Combined reducer can be made to only accept known actions.

test/typescript/tsconfig.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"module": "esnext",
77
"moduleResolution": "node",
88
"emitDeclarationOnly": false,
9-
"strict": false,
9+
"strict": true,
1010
"noEmit": true,
1111
"target": "esnext",
1212
"jsx": "react",

0 commit comments

Comments
 (0)