Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable strict mode for TypeScript tests #4484

Merged
merged 1 commit into from
Feb 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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