diff --git a/src/types/store.ts b/src/types/store.ts index c477cb6471..f071dcb136 100644 --- a/src/types/store.ts +++ b/src/types/store.ts @@ -120,7 +120,11 @@ export type Observer = { * @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 { +export interface Store< + S = any, + A extends Action = AnyAction, + StateExt extends {} = {} +> { /** * Dispatches an action. It is the only way to trigger a state change. * diff --git a/test/typescript/enhancers.ts b/test/typescript/enhancers.ts index 5acdbb8d0a..ab6775dfd9 100644 --- a/test/typescript/enhancers.ts +++ b/test/typescript/enhancers.ts @@ -3,8 +3,7 @@ import { Action, AnyAction, Reducer, - createStore, - Store + createStore } from '../../src' interface State { @@ -157,17 +156,23 @@ function replaceReducerExtender() { } } + interface PartialState { + someField?: 'string' + test?: boolean + } + + const initialReducer: Reducer> = () => ({ + someField: 'string' + }) const store = createStore< - { someField?: 'string'; test?: boolean }, + PartialState, Action, { 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 @@ -234,23 +239,27 @@ function mhelmersonExample() { } } - const store = createStore< - { someField?: 'string'; test?: boolean }, - Action, - {}, - ExtraState - >(reducer, enhancer) - store.replaceReducer(reducer) + interface PartialState { + someField?: 'string' + test?: boolean + } + + const initialReducer: Reducer> = () => ({ + someField: 'string' + }) + const store = createStore, {}, 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 @@ -304,21 +313,25 @@ function finalHelmersonExample() { } } - const store = createStore< - { someField?: 'string'; test?: boolean }, - Action, - {}, - ExtraState - >(reducer, createPersistEnhancer('hi')) + interface PartialState { + someField?: 'string' + test?: boolean + } + + const initialReducer: Reducer> = () => ({ + someField: 'string' + }) + const store = createStore, {}, 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 diff --git a/test/typescript/reducers.ts b/test/typescript/reducers.ts index 6017e0790b..40704fdc2f 100644 --- a/test/typescript/reducers.ts +++ b/test/typescript/reducers.ts @@ -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. diff --git a/test/typescript/tsconfig.json b/test/typescript/tsconfig.json index c13ac0f66b..f33e60206a 100644 --- a/test/typescript/tsconfig.json +++ b/test/typescript/tsconfig.json @@ -6,7 +6,7 @@ "module": "esnext", "moduleResolution": "node", "emitDeclarationOnly": false, - "strict": false, + "strict": true, "noEmit": true, "target": "esnext", "jsx": "react",