Skip to content

Commit 5b081a0

Browse files
committed
Fix issues related to the @typescript-eslint/prefer-function-type rule
1 parent 87e4f55 commit 5b081a0

File tree

11 files changed

+46
-72
lines changed

11 files changed

+46
-72
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
@@ -94,7 +94,7 @@ export type InternalHandlerBuilder<ReturnType = void> = (
9494
input: BuildSubMiddlewareInput,
9595
) => ApiMiddlewareInternalHandler<ReturnType>
9696

97-
export interface PromiseConstructorWithKnownReason {
97+
export type PromiseConstructorWithKnownReason =
9898
/**
9999
* Creates a new Promise with a known rejection reason.
100100
* @param executor A callback used to initialize the promise. This callback is passed two arguments:
@@ -106,8 +106,7 @@ export interface PromiseConstructorWithKnownReason {
106106
resolve: (value: T | PromiseLike<T>) => void,
107107
reject: (reason?: R) => void,
108108
) => void,
109-
): PromiseWithKnownReason<T, R>
110-
}
109+
) => PromiseWithKnownReason<T, R>
111110

112111
export type PromiseWithKnownReason<T, R> = Omit<
113112
Promise<T>,

packages/toolkit/src/query/createApi.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ export interface CreateApiOptions<
301301
skipSchemaValidation?: boolean
302302
}
303303

304-
export type CreateApi<Modules extends ModuleName> = {
304+
export type CreateApi<Modules extends ModuleName> =
305305
/**
306306
* Creates a service to use in your application. Contains only the basic redux logic (the core module).
307307
*
@@ -314,8 +314,7 @@ export type CreateApi<Modules extends ModuleName> = {
314314
TagTypes extends string = never,
315315
>(
316316
options: CreateApiOptions<BaseQuery, Definitions, ReducerPath, TagTypes>,
317-
): Api<BaseQuery, Definitions, ReducerPath, TagTypes, Modules>
318-
}
317+
) => Api<BaseQuery, Definitions, ReducerPath, TagTypes, Modules>
319318

320319
/**
321320
* Builds a `createApi` method based on the provided `modules`.

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

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ export type TypedUseLazyQueryStateResult<
318318
R
319319
>
320320

321-
export type LazyQueryTrigger<D extends QueryDefinition<any, any, any, any>> = {
321+
export type LazyQueryTrigger<D extends QueryDefinition<any, any, any, any>> =
322322
/**
323323
* Triggers a lazy query.
324324
*
@@ -342,8 +342,7 @@ export type LazyQueryTrigger<D extends QueryDefinition<any, any, any, any>> = {
342342
(
343343
arg: QueryArgFrom<D>,
344344
preferCacheValue?: boolean,
345-
): QueryActionCreatorResult<D>
346-
}
345+
) => QueryActionCreatorResult<D>
347346

348347
export type TypedLazyQueryTrigger<
349348
ResultType,
@@ -766,7 +765,7 @@ type UseQueryStateDefaultResult<D extends QueryDefinition<any, any, any, any>> =
766765

767766
export type LazyInfiniteQueryTrigger<
768767
D extends InfiniteQueryDefinition<any, any, any, any, any>,
769-
> = {
768+
> =
770769
/**
771770
* Triggers a lazy query.
772771
*
@@ -790,8 +789,7 @@ export type LazyInfiniteQueryTrigger<
790789
(
791790
arg: QueryArgFrom<D>,
792791
direction: InfiniteQueryDirection,
793-
): InfiniteQueryActionCreatorResult<D>
794-
}
792+
) => InfiniteQueryActionCreatorResult<D>
795793

796794
export type TypedLazyInfiniteQueryTrigger<
797795
ResultType,
@@ -1356,25 +1354,23 @@ export type TypedUseMutation<
13561354
>
13571355

13581356
export type MutationTrigger<D extends MutationDefinition<any, any, any, any>> =
1359-
{
1360-
/**
1361-
* Triggers the mutation and returns a Promise.
1362-
* @remarks
1363-
* If you need to access the error or success payload immediately after a mutation, you can chain .unwrap().
1364-
*
1365-
* @example
1366-
* ```ts
1367-
* // codeblock-meta title="Using .unwrap with async await"
1368-
* try {
1369-
* const payload = await addPost({ id: 1, name: 'Example' }).unwrap();
1370-
* console.log('fulfilled', payload)
1371-
* } catch (error) {
1372-
* console.error('rejected', error);
1373-
* }
1374-
* ```
1375-
*/
1376-
(arg: QueryArgFrom<D>): MutationActionCreatorResult<D>
1377-
}
1357+
/**
1358+
* Triggers the mutation and returns a Promise.
1359+
* @remarks
1360+
* If you need to access the error or success payload immediately after a mutation, you can chain .unwrap().
1361+
*
1362+
* @example
1363+
* ```ts
1364+
* // codeblock-meta title="Using .unwrap with async await"
1365+
* try {
1366+
* const payload = await addPost({ id: 1, name: 'Example' }).unwrap();
1367+
* console.log('fulfilled', payload)
1368+
* } catch (error) {
1369+
* console.error('rejected', error);
1370+
* }
1371+
* ```
1372+
*/
1373+
(arg: QueryArgFrom<D>) => MutationActionCreatorResult<D>
13781374

13791375
export type TypedMutationTrigger<
13801376
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 } from '../tsHelpers'
1412

1513
const getDefaultMiddleware = buildGetDefaultMiddleware()
@@ -76,9 +74,7 @@ describe('getDefaultMiddleware', () => {
7674
})
7775

7876
const dummyMiddleware: Middleware<
79-
{
80-
(action: Action<'actionListenerMiddleware/add'>): () => void
81-
},
77+
(action: Action<'actionListenerMiddleware/add'>) => () => void,
8278
{ counter: number }
8379
> = (storeApi) => (next) => (action) => {
8480
return next(action)

0 commit comments

Comments
 (0)