Skip to content

Commit 75a3786

Browse files
committed
Replace all Function references with AnyFunction
1 parent dbf45d3 commit 75a3786

10 files changed

+107
-56
lines changed

docs/api/actionCreatorMiddleware.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ import reducer from './reducer'
5454
// Augment middleware to consider all functions with a static type property to be action creators
5555
const isActionCreator = (
5656
action: unknown,
57-
): action is Function & { type: unknown } =>
57+
): action is (...args: any) => any & { type: unknown } =>
5858
typeof action === 'function' && 'type' in action
5959

6060
const actionCreatorMiddleware = createActionCreatorInvariantMiddleware({

packages/toolkit/src/actionCreatorInvariantMiddleware.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,23 @@
11
import type { Middleware } from 'redux'
22
import { isActionCreator as isRTKAction } from './createAction'
3+
import type { AnyFunction } from './tsHelpers'
34

45
export interface ActionCreatorInvariantMiddlewareOptions {
56
/**
67
* The function to identify whether a value is an action creator.
78
* The default checks for a function with a static type property and match method.
89
*/
9-
isActionCreator?: (action: unknown) => action is Function & { type?: unknown }
10+
isActionCreator?: (
11+
action: unknown,
12+
) => action is AnyFunction & { type?: unknown }
1013
}
1114

1215
export function getMessage(type?: unknown) {
1316
const splitType = type ? `${type}`.split('/') : []
1417
const actionName = splitType[splitType.length - 1] || 'actionCreator'
1518
return `Detected an action creator with type "${
1619
type || 'unknown'
17-
}" being dispatched.
20+
}" being dispatched.
1821
Make sure you're calling the action creator before dispatching, i.e. \`dispatch(${actionName}())\` instead of \`dispatch(${actionName})\`. This is necessary even if the action has no payload.`
1922
}
2023

packages/toolkit/src/createAction.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import { isAction } from 'redux'
22
import type {
3-
IsUnknownOrNonInferrable,
3+
AnyFunction,
44
IfMaybeUndefined,
55
IfVoid,
66
IsAny,
7+
IsUnknownOrNonInferrable,
78
} from './tsHelpers'
89
import { hasMatchFunction } from './tsHelpers'
910

@@ -257,7 +258,7 @@ export function createAction<
257258
prepareAction: PA,
258259
): PayloadActionCreator<ReturnType<PA>['payload'], T, PA>
259260

260-
export function createAction(type: string, prepareAction?: Function): any {
261+
export function createAction(type: string, prepareAction?: AnyFunction): any {
261262
function actionCreator(...args: any[]) {
262263
if (prepareAction) {
263264
const prepared = prepareAction(...args)
@@ -290,7 +291,7 @@ export function createAction(type: string, prepareAction?: Function): any {
290291
*/
291292
export function isActionCreator(
292293
action: unknown,
293-
): action is BaseActionCreator<unknown, string> & Function {
294+
): action is BaseActionCreator<unknown, string> & AnyFunction {
294295
return (
295296
typeof action === 'function' &&
296297
'type' in action &&

packages/toolkit/src/createReducer.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { produce as createNextState, isDraft, isDraftable } from 'immer'
33
import type { Action, Reducer, UnknownAction } from 'redux'
44
import type { ActionReducerMapBuilder } from './mapBuilders'
55
import { executeReducerBuilderCallback } from './mapBuilders'
6-
import type { NoInfer, TypeGuard } from './tsHelpers'
6+
import type { AnyFunction, NoInfer, TypeGuard } from './tsHelpers'
77
import { freezeDraftable } from './utils'
88

99
/**
@@ -63,7 +63,7 @@ export type CaseReducers<S, AS extends Actions> = {
6363
[T in keyof AS]: AS[T] extends Action ? CaseReducer<S, AS[T]> : void
6464
}
6565

66-
export type NotFunction<T> = T extends Function ? never : T
66+
export type NotFunction<T> = T extends AnyFunction ? never : T
6767

6868
function isStateFunction<S>(x: unknown): x is () => S {
6969
return typeof x === 'function'

packages/toolkit/src/createSlice.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import type {
2525
import { createReducer } from './createReducer'
2626
import type { ActionReducerMapBuilder, TypedActionCreator } from './mapBuilders'
2727
import { executeReducerBuilderCallback } from './mapBuilders'
28-
import type { Id, TypeGuard } from './tsHelpers'
28+
import type { AnyFunction, Id, TypeGuard } from './tsHelpers'
2929
import { getOrInsertComputed } from './utils'
3030

3131
const asyncThunkSymbol = /* @__PURE__ */ Symbol.for(
@@ -876,7 +876,7 @@ interface ReducerHandlingContext<State> {
876876
>
877877
sliceCaseReducersByType: Record<string, CaseReducer<State, any>>
878878
sliceMatchers: ActionMatcherDescriptionCollection<State>
879-
actionCreators: Record<string, Function>
879+
actionCreators: Record<string, AnyFunction>
880880
}
881881

882882
interface ReducerHandlingContextMethods<State> {
@@ -927,7 +927,7 @@ interface ReducerHandlingContextMethods<State> {
927927
*/
928928
exposeAction(
929929
name: string,
930-
actionCreator: Function,
930+
actionCreator: AnyFunction,
931931
): ReducerHandlingContextMethods<State>
932932
/**
933933
* Add a case reducer to be exposed under the final `slice.caseReducers` key.

packages/toolkit/src/devtoolsExtension.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { Action, ActionCreator, StoreEnhancer } from 'redux'
22
import { compose } from 'redux'
3+
import type { AnyFunction } from './tsHelpers'
34

45
/**
56
* @public
@@ -223,7 +224,7 @@ export const composeWithDevTools: ComposeWithDevTools =
223224
: function () {
224225
if (arguments.length === 0) return undefined
225226
if (typeof arguments[0] === 'object') return compose
226-
return compose.apply(null, arguments as any as Function[])
227+
return compose.apply(null, arguments as any as AnyFunction[])
227228
}
228229

229230
/**

packages/toolkit/src/query/endpointDefinitions.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { Api } from '@reduxjs/toolkit/query'
22
import type { StandardSchemaV1 } from '@standard-schema/spec'
3+
import type { AnyFunction } from '../tsHelpers'
34
import type {
45
BaseQueryApi,
56
BaseQueryArg,
@@ -29,6 +30,7 @@ import type {
2930
} from './core/index'
3031
import type { SerializeQueryArgs } from './defaultSerializeQueryArgs'
3132
import type { NEVER } from './fakeBaseQuery'
33+
import type { NamedSchemaError } from './standardSchema'
3234
import type {
3335
CastAny,
3436
HasRequiredProps,
@@ -38,7 +40,6 @@ import type {
3840
UnwrapPromise,
3941
} from './tsHelpers'
4042
import { isNotNullish } from './utils'
41-
import type { NamedSchemaError } from './standardSchema'
4243

4344
const rawResultType = /* @__PURE__ */ Symbol()
4445
const resultType = /* @__PURE__ */ Symbol()
@@ -872,18 +873,18 @@ export interface InfiniteQueryExtraOptions<
872873
* `initialPageParam` may be specified when using the
873874
* endpoint, to override the default value.
874875
* `maxPages` and `getPreviousPageParam` are both optional.
875-
*
876+
*
876877
* @example
877-
*
878+
*
878879
* ```ts
879880
* // codeblock-meta title="infiniteQueryOptions example"
880881
* import { createApi, fetchBaseQuery, defaultSerializeQueryArgs } from '@reduxjs/toolkit/query/react'
881-
*
882+
*
882883
* type Pokemon = {
883884
* id: string
884885
* name: string
885886
* }
886-
*
887+
*
887888
* const pokemonApi = createApi({
888889
* baseQuery: fetchBaseQuery({ baseUrl: 'https://pokeapi.co/api/v2/' }),
889890
* endpoints: (build) => ({
@@ -908,7 +909,7 @@ export interface InfiniteQueryExtraOptions<
908909
* }),
909910
* }),
910911
* })
911-
912+
912913
* ```
913914
*/
914915
infiniteQueryOptions: InfiniteQueryConfigOptions<
@@ -1371,7 +1372,7 @@ export function calculateProvidedBy<ResultType, QueryArg, ErrorType, MetaType>(
13711372
return []
13721373
}
13731374

1374-
function isFunction<T>(t: T): t is Extract<T, Function> {
1375+
function isFunction<T>(t: T): t is Extract<T, AnyFunction> {
13751376
return typeof t === 'function'
13761377
}
13771378

packages/toolkit/src/tests/actionCreatorInvariantMiddleware.test.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
import type { ActionCreatorInvariantMiddlewareOptions } from '@internal/actionCreatorInvariantMiddleware'
2-
import { getMessage } from '@internal/actionCreatorInvariantMiddleware'
3-
import { createActionCreatorInvariantMiddleware } from '@internal/actionCreatorInvariantMiddleware'
2+
import {
3+
createActionCreatorInvariantMiddleware,
4+
getMessage,
5+
} from '@internal/actionCreatorInvariantMiddleware'
46
import type { MiddlewareAPI } from '@reduxjs/toolkit'
57
import { createAction } from '@reduxjs/toolkit'
8+
import type { AnyFunction } from '../tsHelpers'
69

710
describe('createActionCreatorInvariantMiddleware', () => {
811
const consoleSpy = vi.spyOn(console, 'warn').mockImplementation(() => {})
@@ -50,7 +53,7 @@ describe('createActionCreatorInvariantMiddleware', () => {
5053
it('allows passing a custom predicate', () => {
5154
let predicateCalled = false
5255
const testAction = makeActionTester({
53-
isActionCreator(action): action is Function {
56+
isActionCreator(action): action is AnyFunction {
5457
predicateCalled = true
5558
return false
5659
},

0 commit comments

Comments
 (0)