Skip to content

Commit

Permalink
replace any by unknown in Action and AnyAction
Browse files Browse the repository at this point in the history
  • Loading branch information
guillaumebrunerie committed Apr 16, 2023
1 parent 461b093 commit 435fd41
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 16 deletions.
6 changes: 3 additions & 3 deletions src/combineReducers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,16 +111,16 @@ function assertReducerShape(reducers: ReducersMapObject) {
* object, and builds a state object with the same shape.
*/
export default function combineReducers<S>(
reducers: ReducersMapObject<S, any>
reducers: ReducersMapObject<S>
): Reducer<CombinedState<S>>
export default function combineReducers<S, A extends Action = AnyAction>(
reducers: ReducersMapObject<S, A>
): Reducer<CombinedState<S>, A>
export default function combineReducers<M extends ReducersMapObject>(
export default function combineReducers<M extends ReducersMapObject<S>, S>(
reducers: M
): Reducer<
CombinedState<StateFromReducersMapObject<M>>,
ActionFromReducersMapObject<M>
ActionFromReducersMapObject<M, S>
>
export default function combineReducers(reducers: ReducersMapObject) {
const reducerKeys = Object.keys(reducers)
Expand Down
4 changes: 2 additions & 2 deletions src/types/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*
* @template T the type of the action's `type` tag.
*/
export interface Action<T = any> {
export type Action<T = unknown> = {
type: T
}

Expand All @@ -27,7 +27,7 @@ export interface Action<T = any> {
*/
export interface AnyAction extends Action {
// Allows any extra properties to be defined in an action.
[extraProps: string]: any
[extraProps: string]: unknown
}

/* action creators */
Expand Down
6 changes: 3 additions & 3 deletions src/types/middleware.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Dispatch } from './store'

export interface MiddlewareAPI<D extends Dispatch = Dispatch, S = any> {
export interface MiddlewareAPI<D extends Dispatch = Dispatch, S = unknown> {
dispatch: D
getState(): S
}
Expand All @@ -21,10 +21,10 @@ export interface MiddlewareAPI<D extends Dispatch = Dispatch, S = any> {
*/
export interface Middleware<
_DispatchExt = {}, // TODO: remove unused component (breaking change)
S = any,
S = unknown,
D extends Dispatch = Dispatch
> {
(api: MiddlewareAPI<D, S>): (
next: D
) => (action: D extends Dispatch<infer A> ? A : never) => any
) => (action: D extends Dispatch<infer A> ? A : never) => unknown
}
4 changes: 2 additions & 2 deletions src/types/reducers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import { Action, AnyAction } from './actions'
* @template S The type of state consumed and produced by this reducer.
* @template A The type of actions the reducer can potentially respond to.
*/
export type Reducer<S = any, A extends Action = AnyAction> = (
export type Reducer<S = unknown, A extends Action = AnyAction> = (
state: S | undefined,
action: A
) => S
Expand Down Expand Up @@ -74,6 +74,6 @@ export type ActionFromReducer<R> = R extends Reducer<any, infer A> ? A : never
*
* @template M Object map of reducers as provided to `combineReducers(map: M)`.
*/
export type ActionFromReducersMapObject<M> = M extends ReducersMapObject
export type ActionFromReducersMapObject<M, S> = M extends ReducersMapObject<S>
? ActionFromReducer<ReducerFromReducersMapObject<M>>
: never
4 changes: 2 additions & 2 deletions src/types/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ export type Observer<T> = {
* @template StateExt any extension to state from store enhancers
*/
export interface Store<
S = any,
S = unknown,
A extends Action = AnyAction,
StateExt extends {} = {}
> {
Expand Down Expand Up @@ -260,7 +260,7 @@ export type StoreEnhancer<Ext extends {} = {}, StateExt extends {} = {}> = <
export type StoreEnhancerStoreCreator<
Ext extends {} = {},
StateExt extends {} = {}
> = <S = any, A extends Action = AnyAction>(
> = <S = unknown, A extends Action = AnyAction>(
reducer: Reducer<S, A>,
preloadedState?: PreloadedState<S>
) => Store<S, A, StateExt> & Ext
12 changes: 8 additions & 4 deletions test/typescript/reducers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,17 @@ function simple() {
if (action.type === 'INCREMENT') {
const { count = 1 } = action

return state + count
if (typeof count == 'number') {
return state + count
}
}

if (action.type === 'DECREMENT') {
const { count = 1 } = action

return state - count
if (typeof count == 'number') {
return state - count
}
}

return state
Expand Down Expand Up @@ -176,12 +180,12 @@ function typeGuards() {

type State = number

interface IncrementAction {
type IncrementAction = {
type: 'INCREMENT'
count?: number
}

interface DecrementAction {
type DecrementAction = {
type: 'DECREMENT'
count?: number
}
Expand Down

0 comments on commit 435fd41

Please sign in to comment.