Skip to content

Commit

Permalink
Merge pull request #4484 from Methuselah96/typescript-tests-strict
Browse files Browse the repository at this point in the history
  • Loading branch information
markerikson authored Feb 12, 2023
2 parents db753b1 + 08a38de commit 792d0b4
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 33 deletions.
6 changes: 5 additions & 1 deletion src/types/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,11 @@ export type Observer<T> = {
* @template A the type of actions which may be dispatched by this store.
* @template StateExt any extension to state from store enhancers
*/
export interface Store<S = any, A extends Action = AnyAction, StateExt = {}> {
export interface Store<
S = any,
A extends Action = AnyAction,
StateExt extends {} = {}
> {
/**
* Dispatches an action. It is the only way to trigger a state change.
*
Expand Down
71 changes: 42 additions & 29 deletions test/typescript/enhancers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ import {
Action,
AnyAction,
Reducer,
createStore,
Store
createStore
} from '../../src'

interface State {
Expand Down Expand Up @@ -157,17 +156,23 @@ function replaceReducerExtender() {
}
}

interface PartialState {
someField?: 'string'
test?: boolean
}

const initialReducer: Reducer<PartialState, Action<unknown>> = () => ({
someField: 'string'
})
const store = createStore<
{ someField?: 'string'; test?: boolean },
PartialState,
Action<unknown>,
{ method(): string },
ExtraState
>(reducer, enhancer)
>(initialReducer, enhancer)

const newReducer = (
state: { test: boolean } = { test: true },
_: AnyAction
) => state
const newReducer = (state: PartialState = { test: true }, _: AnyAction) =>
state

store.replaceReducer(newReducer)
store.getState().test
Expand Down Expand Up @@ -234,23 +239,27 @@ function mhelmersonExample() {
}
}

const store = createStore<
{ someField?: 'string'; test?: boolean },
Action<unknown>,
{},
ExtraState
>(reducer, enhancer)
store.replaceReducer(reducer)
interface PartialState {
someField?: 'string'
test?: boolean
}

const initialReducer: Reducer<PartialState, Action<unknown>> = () => ({
someField: 'string'
})
const store = createStore<PartialState, Action<unknown>, {}, ExtraState>(
initialReducer,
enhancer
)
store.replaceReducer(initialReducer)

store.getState().extraField
// @ts-expect-error
store.getState().wrongField
store.getState().test

const newReducer = (
state: { test: boolean } = { test: true },
_: AnyAction
) => state
const newReducer = (state: PartialState = { test: true }, _: AnyAction) =>
state

store.replaceReducer(newReducer)
store.getState().test
Expand Down Expand Up @@ -304,21 +313,25 @@ function finalHelmersonExample() {
}
}

const store = createStore<
{ someField?: 'string'; test?: boolean },
Action<unknown>,
{},
ExtraState
>(reducer, createPersistEnhancer('hi'))
interface PartialState {
someField?: 'string'
test?: boolean
}

const initialReducer: Reducer<PartialState, Action<unknown>> = () => ({
someField: 'string'
})
const store = createStore<PartialState, Action<unknown>, {}, ExtraState>(
initialReducer,
createPersistEnhancer('hi')
)

store.getState().foo
// @ts-expect-error
store.getState().wrongField

const newReducer = (
state: { test: boolean } = { test: true },
_: AnyAction
) => state
const newReducer = (state: PartialState = { test: true }, _: AnyAction) =>
state

store.replaceReducer(newReducer)
store.getState().test
Expand Down
4 changes: 2 additions & 2 deletions test/typescript/reducers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,9 @@ function discriminated() {

const cs = combined(undefined, { type: 'INCREMENT' })
combined(cs, { type: 'MULTIPLY' })
// TODO // @ts-expect-error
// @ts-expect-error
combined(cs, { type: 'init' })
// TODO // @ts-expect-error
// @ts-expect-error
combined(cs, { type: 'SOME_OTHER_TYPE' })

// Combined reducer can be made to only accept known actions.
Expand Down
2 changes: 1 addition & 1 deletion test/typescript/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"module": "esnext",
"moduleResolution": "node",
"emitDeclarationOnly": false,
"strict": false,
"strict": true,
"noEmit": true,
"target": "esnext",
"jsx": "react",
Expand Down

0 comments on commit 792d0b4

Please sign in to comment.