Skip to content

Commit a579584

Browse files
committed
Fix issues related to the @typescript-eslint/prefer-function-type rule
1 parent 7d241a8 commit a579584

File tree

11 files changed

+44
-69
lines changed

11 files changed

+44
-69
lines changed

packages/toolkit/src/devtoolsExtension.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export interface DevToolsEnhancerOptions {
1414
/**
1515
* action creators functions to be available in the Dispatcher.
1616
*/
17-
actionCreators?: ActionCreator<any>[] | { [key: string]: ActionCreator<any> }
17+
actionCreators?: ActionCreator<any>[] | Record<string, ActionCreator<any>>
1818
/**
1919
* if more than one action is dispatched in the indicated interval, all new actions will be collected and sent at once.
2020
* It is the joint between performance and speed. When set to `0`, all actions will be sent instantly.
@@ -232,9 +232,9 @@ export const composeWithDevTools: ComposeWithDevTools =
232232
/**
233233
* @public
234234
*/
235-
export const devToolsEnhancer: {
236-
(options: DevToolsEnhancerOptions): StoreEnhancer<any>
237-
} =
235+
export const devToolsEnhancer: (
236+
options: DevToolsEnhancerOptions,
237+
) => StoreEnhancer<any> =
238238
typeof window !== 'undefined' && (window as any).__REDUX_DEVTOOLS_EXTENSION__
239239
? (window as any).__REDUX_DEVTOOLS_EXTENSION__
240240
: function () {

packages/toolkit/src/dynamicMiddleware/tests/index.test.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import type { Middleware } from 'redux'
2-
import { createDynamicMiddleware } from '../index'
32
import { configureStore } from '../../configureStore'
43
import type { BaseActionCreator, PayloadAction } from '../../createAction'
54
import { createAction } from '../../createAction'
65
import { isAllOf } from '../../matchers'
6+
import { createDynamicMiddleware } from '../index'
77

88
const probeType = 'probeableMW/probe'
99

@@ -21,9 +21,7 @@ const matchId =
2121

2222
export const makeProbeableMiddleware = <Id extends number>(
2323
id: Id,
24-
): Middleware<{
25-
(action: PayloadAction<Id, typeof probeType>): Id
26-
}> => {
24+
): Middleware<(action: PayloadAction<Id, typeof probeType>) => Id> => {
2725
const isMiddlewareAction = isAllOf(probeMiddleware, matchId(id))
2826
return (api) => (next) => (action) => {
2927
if (isMiddlewareAction(action)) {

packages/toolkit/src/dynamicMiddleware/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ export interface DynamicDispatch {
5252
// return a version of dispatch that knows about middleware
5353
<Middlewares extends Middleware<any>[]>(
5454
action: PayloadAction<Middlewares, 'dynamicMiddleware/add'>,
55+
// eslint-disable-next-line @typescript-eslint/prefer-function-type
5556
): ExtractDispatchExtensions<Middlewares> & this
5657
}
5758

packages/toolkit/src/listenerMiddleware/types.ts

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -72,14 +72,10 @@ export interface ForkedTaskAPI {
7272
}
7373

7474
/** @public */
75-
export interface AsyncTaskExecutor<T> {
76-
(forkApi: ForkedTaskAPI): Promise<T>
77-
}
75+
export type AsyncTaskExecutor<T> = (forkApi: ForkedTaskAPI) => Promise<T>
7876

7977
/** @public */
80-
export interface SyncTaskExecutor<T> {
81-
(forkApi: ForkedTaskAPI): T
82-
}
78+
export type SyncTaskExecutor<T> = (forkApi: ForkedTaskAPI) => T
8379

8480
/** @public */
8581
export type ForkedTaskExecutor<T> = AsyncTaskExecutor<T> | SyncTaskExecutor<T>
@@ -295,9 +291,10 @@ export interface ListenerErrorInfo {
295291
* @param error The thrown error.
296292
* @param errorInfo Additional information regarding the thrown error.
297293
*/
298-
export interface ListenerErrorHandler {
299-
(error: unknown, errorInfo: ListenerErrorInfo): void
300-
}
294+
export type ListenerErrorHandler = (
295+
error: unknown,
296+
errorInfo: ListenerErrorInfo,
297+
) => void
301298

302299
/** @public */
303300
export interface CreateListenerMiddlewareOptions<ExtraArgument = unknown> {
@@ -318,9 +315,7 @@ export type ListenerMiddleware<
318315
>,
319316
ExtraArgument = unknown,
320317
> = Middleware<
321-
{
322-
(action: Action<'listenerMiddleware/add'>): UnsubscribeListener
323-
},
318+
(action: Action<'listenerMiddleware/add'>) => UnsubscribeListener,
324319
State,
325320
DispatchType
326321
>

packages/toolkit/src/query/core/buildMiddleware/types.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ export type InternalHandlerBuilder<ReturnType = void> = (
8989
input: BuildSubMiddlewareInput,
9090
) => ApiMiddlewareInternalHandler<ReturnType>
9191

92-
export interface PromiseConstructorWithKnownReason {
92+
export type PromiseConstructorWithKnownReason =
9393
/**
9494
* Creates a new Promise with a known rejection reason.
9595
* @param executor A callback used to initialize the promise. This callback is passed two arguments:
@@ -101,8 +101,7 @@ export interface PromiseConstructorWithKnownReason {
101101
resolve: (value: T | PromiseLike<T>) => void,
102102
reject: (reason?: R) => void,
103103
) => void,
104-
): PromiseWithKnownReason<T, R>
105-
}
104+
) => PromiseWithKnownReason<T, R>
106105

107106
export type PromiseWithKnownReason<T, R> = Omit<
108107
Promise<T>,

packages/toolkit/src/query/createApi.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ export interface CreateApiOptions<
210210
>
211211
}
212212

213-
export type CreateApi<Modules extends ModuleName> = {
213+
export type CreateApi<Modules extends ModuleName> =
214214
/**
215215
* Creates a service to use in your application. Contains only the basic redux logic (the core module).
216216
*
@@ -223,8 +223,7 @@ export type CreateApi<Modules extends ModuleName> = {
223223
TagTypes extends string = never,
224224
>(
225225
options: CreateApiOptions<BaseQuery, Definitions, ReducerPath, TagTypes>,
226-
): Api<BaseQuery, Definitions, ReducerPath, TagTypes, Modules>
227-
}
226+
) => Api<BaseQuery, Definitions, ReducerPath, TagTypes, Modules>
228227

229228
/**
230229
* Builds a `createApi` method based on the provided `modules`.

packages/toolkit/src/query/react/buildHooks.ts

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ export type TypedUseLazyQuery<
267267
QueryDefinition<QueryArg, BaseQuery, string, ResultType, string>
268268
>
269269

270-
export type LazyQueryTrigger<D extends QueryDefinition<any, any, any, any>> = {
270+
export type LazyQueryTrigger<D extends QueryDefinition<any, any, any, any>> =
271271
/**
272272
* Triggers a lazy query.
273273
*
@@ -291,8 +291,7 @@ export type LazyQueryTrigger<D extends QueryDefinition<any, any, any, any>> = {
291291
(
292292
arg: QueryArgFrom<D>,
293293
preferCacheValue?: boolean,
294-
): QueryActionCreatorResult<D>
295-
}
294+
) => QueryActionCreatorResult<D>
296295

297296
export type TypedLazyQueryTrigger<
298297
ResultType,
@@ -583,25 +582,23 @@ export type TypedUseMutation<
583582
>
584583

585584
export type MutationTrigger<D extends MutationDefinition<any, any, any, any>> =
586-
{
587-
/**
588-
* Triggers the mutation and returns a Promise.
589-
* @remarks
590-
* If you need to access the error or success payload immediately after a mutation, you can chain .unwrap().
591-
*
592-
* @example
593-
* ```ts
594-
* // codeblock-meta title="Using .unwrap with async await"
595-
* try {
596-
* const payload = await addPost({ id: 1, name: 'Example' }).unwrap();
597-
* console.log('fulfilled', payload)
598-
* } catch (error) {
599-
* console.error('rejected', error);
600-
* }
601-
* ```
602-
*/
603-
(arg: QueryArgFrom<D>): MutationActionCreatorResult<D>
604-
}
585+
/**
586+
* Triggers the mutation and returns a Promise.
587+
* @remarks
588+
* If you need to access the error or success payload immediately after a mutation, you can chain .unwrap().
589+
*
590+
* @example
591+
* ```ts
592+
* // codeblock-meta title="Using .unwrap with async await"
593+
* try {
594+
* const payload = await addPost({ id: 1, name: 'Example' }).unwrap();
595+
* console.log('fulfilled', payload)
596+
* } catch (error) {
597+
* console.error('rejected', error);
598+
* }
599+
* ```
600+
*/
601+
(arg: QueryArgFrom<D>) => MutationActionCreatorResult<D>
605602

606603
export type TypedMutationTrigger<
607604
ResultType,

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -777,9 +777,7 @@ describe('type tests', () => {
777777
// A fake middleware that tells TS that an unsubscribe callback is being returned for a given action
778778
// This is the same signature that the "listener" middleware uses
779779
const dummyMiddleware: Middleware<
780-
{
781-
(action: Action<'actionListenerMiddleware/add'>): Unsubscribe
782-
},
780+
(action: Action<'actionListenerMiddleware/add'>) => Unsubscribe,
783781
CounterState
784782
> = (storeApi) => (next) => (action) => {}
785783

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

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,9 @@ import type {
1111
} from '@reduxjs/toolkit'
1212
import { configureStore } from '@reduxjs/toolkit'
1313

14-
declare const middleware1: Middleware<{
15-
(_: string): number
16-
}>
14+
declare const middleware1: Middleware<(_: string) => number>
1715

18-
declare const middleware2: Middleware<{
19-
(_: number): string
20-
}>
16+
declare const middleware2: Middleware<(_: number) => string>
2117

2218
type ThunkReturn = Promise<'thunk'>
2319
declare const thunkCreator: () => () => ThunkReturn
@@ -136,9 +132,7 @@ describe('type tests', () => {
136132
expectTypeOf(m2).toMatchTypeOf<Tuple<[]>>()
137133

138134
const dummyMiddleware: Middleware<
139-
{
140-
(action: Action<'actionListenerMiddleware/add'>): () => void
141-
},
135+
(action: Action<'actionListenerMiddleware/add'>) => () => void,
142136
{ counter: number }
143137
> = (storeApi) => (next) => (action) => {
144138
return next(action)

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

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { buildGetDefaultMiddleware } from '@internal/getDefaultMiddleware'
12
import { Tuple } from '@internal/utils'
23
import type {
34
Action,
@@ -7,9 +8,6 @@ import type {
78
} from '@reduxjs/toolkit'
89
import { configureStore } from '@reduxjs/toolkit'
910
import { thunk } from 'redux-thunk'
10-
import { vi } from 'vitest'
11-
12-
import { buildGetDefaultMiddleware } from '@internal/getDefaultMiddleware'
1311
import type { AnyFunction, AnyNonNullishValue } from '../tsHelpers'
1412

1513
const getDefaultMiddleware = buildGetDefaultMiddleware()
@@ -77,9 +75,7 @@ describe('getDefaultMiddleware', () => {
7775
})
7876

7977
const dummyMiddleware: Middleware<
80-
{
81-
(action: Action<'actionListenerMiddleware/add'>): () => void
82-
},
78+
(action: Action<'actionListenerMiddleware/add'>) => () => void,
8379
{ counter: number }
8480
> = (storeApi) => (next) => (action) => {
8581
return next(action)

0 commit comments

Comments
 (0)