Skip to content

Commit 965531a

Browse files
committed
Replace all Function references with AnyFunction
1 parent a47ce6a commit 965531a

9 files changed

+35
-19
lines changed

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: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,12 @@ 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 {
29+
AnyFunction,
30+
AnyNonNullishValue,
31+
Id,
32+
TypeGuard,
33+
} from './tsHelpers'
2934
import { emplace } from './utils'
3035

3136
const asyncThunkSymbol = /* @__PURE__ */ Symbol.for(
@@ -864,7 +869,7 @@ interface ReducerHandlingContext<State> {
864869
>
865870
sliceCaseReducersByType: Record<string, CaseReducer<State, any>>
866871
sliceMatchers: ActionMatcherDescriptionCollection<State>
867-
actionCreators: Record<string, Function>
872+
actionCreators: Record<string, AnyFunction>
868873
}
869874

870875
interface ReducerHandlingContextMethods<State> {
@@ -915,7 +920,7 @@ interface ReducerHandlingContextMethods<State> {
915920
*/
916921
exposeAction(
917922
name: string,
918-
actionCreator: Function,
923+
actionCreator: AnyFunction,
919924
): ReducerHandlingContextMethods<State>
920925
/**
921926
* 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, AnyNonNullishValue } 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: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { Api } from '@reduxjs/toolkit/query'
2+
import type { AnyFunction, AnyNonNullishValue } from '../tsHelpers'
23
import type {
34
BaseQueryApi,
45
BaseQueryArg,
@@ -783,7 +784,7 @@ export function calculateProvidedBy<ResultType, QueryArg, ErrorType, MetaType>(
783784
return []
784785
}
785786

786-
function isFunction<T>(t: T): t is Extract<T, Function> {
787+
function isFunction<T>(t: T): t is Extract<T, AnyFunction> {
787788
return typeof t === 'function'
788789
}
789790

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
},

packages/toolkit/src/tests/createAsyncThunk.test-d.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
} from '@reduxjs/toolkit'
1515
import type { AxiosError } from 'axios'
1616
import apiRequest from 'axios'
17+
import type { AnyFunction, AnyNonNullishValue } from '../tsHelpers'
1718

1819
const defaultDispatch = (() => {}) as ThunkDispatch<{}, any, UnknownAction>
1920
const unknownAction = { type: 'foo' } as UnknownAction
@@ -97,7 +98,7 @@ describe('type tests', () => {
9798

9899
const correctDispatch = (() => {}) as ThunkDispatch<
99100
BookModel[],
100-
{ userAPI: Function },
101+
{ userAPI: AnyFunction },
101102
UnknownAction
102103
>
103104

@@ -107,7 +108,7 @@ describe('type tests', () => {
107108
number,
108109
{
109110
state: BooksState
110-
extra: { userAPI: Function }
111+
extra: { userAPI: AnyFunction }
111112
}
112113
>(
113114
'books/fetch',
@@ -118,7 +119,7 @@ describe('type tests', () => {
118119

119120
expectTypeOf(state).toEqualTypeOf<BookModel[]>()
120121

121-
expectTypeOf(extra).toEqualTypeOf<{ userAPI: Function }>()
122+
expectTypeOf(extra).toEqualTypeOf<{ userAPI: AnyFunction }>()
122123

123124
return fakeBooks
124125
},

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { thunk } from 'redux-thunk'
1010
import { vi } from 'vitest'
1111

1212
import { buildGetDefaultMiddleware } from '@internal/getDefaultMiddleware'
13+
import type { AnyFunction, AnyNonNullishValue } from '../tsHelpers'
1314

1415
const getDefaultMiddleware = buildGetDefaultMiddleware()
1516

@@ -181,7 +182,7 @@ it('allows passing options to actionCreatorCheck', () => {
181182
immutableCheck: false,
182183
serializableCheck: false,
183184
actionCreatorCheck: {
184-
isActionCreator: (action: unknown): action is Function => {
185+
isActionCreator: (action: unknown): action is AnyFunction => {
185186
actionCreatorCheckWasCalled = true
186187
return false
187188
},

0 commit comments

Comments
 (0)