Skip to content

Commit fb02311

Browse files
committed
Add JSDocs for listenerMiddleware.withTypes
1 parent 5b8885b commit fb02311

File tree

4 files changed

+195
-38
lines changed

4 files changed

+195
-38
lines changed

packages/toolkit/src/listenerMiddleware/tests/listenerMiddleware.withTypes.test-d.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import {
1414
createSlice,
1515
removeListener,
1616
} from '@reduxjs/toolkit'
17-
import { expectTypeOf } from 'vitest'
17+
import { describe, expectTypeOf, test } from 'vitest'
1818

1919
export interface CounterState {
2020
counter: number
@@ -140,7 +140,10 @@ describe('listenerMiddleware.withTypes<RootState, AppDispatch>()', () => {
140140
})
141141

142142
test('stopListening.withTypes', () => {
143-
const stopAppListening = listenerMiddleware.stopListening.withTypes<RootState, AppDispatch>()
143+
const stopAppListening = listenerMiddleware.stopListening.withTypes<
144+
RootState,
145+
AppDispatch
146+
>()
144147

145148
expectTypeOf(stopAppListening).toEqualTypeOf<
146149
TypedStopListening<RootState, AppDispatch>

packages/toolkit/src/listenerMiddleware/tests/listenerMiddleware.withTypes.test.ts

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -71,41 +71,45 @@ const addAppListener = addListener.withTypes<RootState, AppDispatch>()
7171

7272
const removeAppListener = removeListener.withTypes<RootState, AppDispatch>()
7373

74-
describe(startAppListening.withTypes, () => {
74+
describe('startAppListening.withTypes', () => {
7575
test('should return startListening', () => {
76-
expect(startAppListening.withTypes).to.be.a('function')
76+
expect(startAppListening.withTypes).toEqual(expect.any(Function))
7777

78-
expect(startAppListening.withTypes().withTypes).to.be.a('function')
78+
expect(startAppListening.withTypes().withTypes).toEqual(
79+
expect.any(Function)
80+
)
7981

8082
expect(startAppListening).toBe(listenerMiddleware.startListening)
8183
})
8284
})
8385

84-
describe(stopAppListening.withTypes, () => {
86+
describe('stopAppListening.withTypes', () => {
8587
test('should return stopListening', () => {
86-
expect(stopAppListening.withTypes).to.be.a('function')
88+
expect(stopAppListening.withTypes).toEqual(expect.any(Function))
8789

88-
expect(stopAppListening.withTypes().withTypes).to.be.a('function')
90+
expect(stopAppListening.withTypes().withTypes).toEqual(expect.any(Function))
8991

9092
expect(stopAppListening).toBe(listenerMiddleware.stopListening)
9193
})
9294
})
9395

94-
describe(addAppListener.withTypes, () => {
96+
describe('addAppListener.withTypes', () => {
9597
test('should return addListener', () => {
96-
expect(addAppListener.withTypes).to.be.a('function')
98+
expect(addAppListener.withTypes).toEqual(expect.any(Function))
9799

98-
expect(addAppListener.withTypes().withTypes).to.be.a('function')
100+
expect(addAppListener.withTypes().withTypes).toEqual(expect.any(Function))
99101

100102
expect(addAppListener).toBe(addListener)
101103
})
102104
})
103105

104-
describe(removeAppListener.withTypes, () => {
106+
describe('removeAppListener.withTypes', () => {
105107
test('should return removeListener', () => {
106-
expect(removeAppListener.withTypes).to.be.a('function')
108+
expect(removeAppListener.withTypes).toEqual(expect.any(Function))
107109

108-
expect(removeAppListener.withTypes().withTypes).to.be.a('function')
110+
expect(removeAppListener.withTypes().withTypes).toEqual(
111+
expect.any(Function)
112+
)
109113

110114
expect(removeAppListener).toBe(removeListener)
111115
})

packages/toolkit/src/listenerMiddleware/types.ts

Lines changed: 173 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -531,8 +531,10 @@ export interface RemoveListenerAction<
531531
}
532532

533533
/**
534+
* A "pre-typed" version of `addListenerAction`, so the listener args are well-typed
535+
*
534536
* @public
535-
* A "pre-typed" version of `addListenerAction`, so the listener args are well-typed */
537+
*/
536538
export type TypedAddListener<
537539
StateType,
538540
DispatchType extends ReduxDispatch = ThunkDispatch<
@@ -550,15 +552,42 @@ export type TypedAddListener<
550552
DispatchType,
551553
ExtraArgument
552554
> & {
555+
/**
556+
* Creates a "pre-typed" version of `addListener`
557+
* where the `state` and `dispatch` types are predefined.
558+
*
559+
* This allows you to set the `state` and `dispatch` types once,
560+
* eliminating the need to specify them with every `addListener` call.
561+
*
562+
* @returns A pre-typed `addListener` with the state and dispatch types already defined.
563+
*
564+
* @example
565+
* ```ts
566+
* import { addListener } from '@reduxjs/toolkit'
567+
*
568+
* export const addAppListener = addListener.withTypes<RootState, AppDispatch>()
569+
* ```
570+
*
571+
* @template OverrideStateType - The specific type of state the middleware listener operates on.
572+
* @template OverrideDispatchType - The specific type of the dispatch function.
573+
*
574+
* @since 2.1.0
575+
*/
553576
withTypes: <
554577
OverrideStateType extends StateType,
555-
OverrideDispatchType extends DispatchType
578+
OverrideDispatchType extends ReduxDispatch = ThunkDispatch<
579+
OverrideStateType,
580+
unknown,
581+
UnknownAction
582+
>
556583
>() => TypedAddListener<OverrideStateType, OverrideDispatchType>
557584
}
558585

559586
/**
587+
* A "pre-typed" version of `removeListenerAction`, so the listener args are well-typed
588+
*
560589
* @public
561-
* A "pre-typed" version of `removeListenerAction`, so the listener args are well-typed */
590+
*/
562591
export type TypedRemoveListener<
563592
StateType,
564593
DispatchType extends ReduxDispatch = ThunkDispatch<
@@ -568,22 +597,50 @@ export type TypedRemoveListener<
568597
>,
569598
Payload = ListenerEntry<StateType, DispatchType>,
570599
T extends string = 'listenerMiddleware/remove'
571-
> = BaseActionCreator<Payload, T> & {
572-
withTypes: <
573-
OverrideStateType extends StateType,
574-
OverrideDispatchType extends DispatchType
575-
>() => TypedRemoveListener<OverrideStateType, OverrideDispatchType>
576-
} & AddListenerOverloads<
600+
> = BaseActionCreator<Payload, T> &
601+
AddListenerOverloads<
577602
PayloadAction<Payload, T>,
578603
StateType,
579604
DispatchType,
580605
any,
581606
UnsubscribeListenerOptions
582-
>
607+
> & {
608+
/**
609+
* Creates a "pre-typed" version of `removeListener`
610+
* where the `state` and `dispatch` types are predefined.
611+
*
612+
* This allows you to set the `state` and `dispatch` types once,
613+
* eliminating the need to specify them with every `removeListener` call.
614+
*
615+
* @returns A pre-typed `removeListener` with the state and dispatch types already defined.
616+
*
617+
* @example
618+
* ```ts
619+
* import { removeListener } from '@reduxjs/toolkit'
620+
*
621+
* export const removeAppListener = removeListener.withTypes<RootState, AppDispatch>()
622+
* ```
623+
*
624+
* @template OverrideStateType - The specific type of state the middleware listener operates on.
625+
* @template OverrideDispatchType - The specific type of the dispatch function.
626+
*
627+
* @since 2.1.0
628+
*/
629+
withTypes: <
630+
OverrideStateType extends StateType,
631+
OverrideDispatchType extends ReduxDispatch = ThunkDispatch<
632+
OverrideStateType,
633+
unknown,
634+
UnknownAction
635+
>
636+
>() => TypedRemoveListener<OverrideStateType, OverrideDispatchType>
637+
}
583638

584639
/**
640+
* A "pre-typed" version of `middleware.startListening`, so the listener args are well-typed
641+
*
585642
* @public
586-
* A "pre-typed" version of `middleware.startListening`, so the listener args are well-typed */
643+
*/
587644
export type TypedStartListening<
588645
StateType,
589646
DispatchType extends ReduxDispatch = ThunkDispatch<
@@ -598,14 +655,49 @@ export type TypedStartListening<
598655
DispatchType,
599656
ExtraArgument
600657
> & {
658+
/**
659+
* Creates a "pre-typed" version of
660+
* {@linkcode ListenerMiddlewareInstance.startListening startListening}
661+
* where the `state` and `dispatch` types are predefined.
662+
*
663+
* This allows you to set the `state` and `dispatch` types once,
664+
* eliminating the need to specify them with every
665+
* {@linkcode ListenerMiddlewareInstance.startListening startListening} call.
666+
*
667+
* @returns A pre-typed `startListening` with the state and dispatch types already defined.
668+
*
669+
* @example
670+
* ```ts
671+
* import { createListenerMiddleware } from '@reduxjs/toolkit'
672+
*
673+
* const listenerMiddleware = createListenerMiddleware()
674+
*
675+
* export const startAppListening = listenerMiddleware.startListening.withTypes<
676+
* RootState,
677+
* AppDispatch
678+
* >()
679+
* ```
680+
*
681+
* @template OverrideStateType - The specific type of state the middleware listener operates on.
682+
* @template OverrideDispatchType - The specific type of the dispatch function.
683+
*
684+
* @since 2.1.0
685+
*/
601686
withTypes: <
602687
OverrideStateType extends StateType,
603-
OverrideDispatchType extends DispatchType
688+
OverrideDispatchType extends ReduxDispatch = ThunkDispatch<
689+
OverrideStateType,
690+
unknown,
691+
UnknownAction
692+
>
604693
>() => TypedStartListening<OverrideStateType, OverrideDispatchType>
605694
}
606695

607-
/** @public
608-
* A "pre-typed" version of `middleware.stopListening`, so the listener args are well-typed */
696+
/**
697+
* A "pre-typed" version of `middleware.stopListening`, so the listener args are well-typed
698+
*
699+
* @public
700+
*/
609701
export type TypedStopListening<
610702
StateType,
611703
DispatchType extends ReduxDispatch = ThunkDispatch<
@@ -614,14 +706,49 @@ export type TypedStopListening<
614706
UnknownAction
615707
>
616708
> = RemoveListenerOverloads<StateType, DispatchType> & {
709+
/**
710+
* Creates a "pre-typed" version of
711+
* {@linkcode ListenerMiddlewareInstance.stopListening stopListening}
712+
* where the `state` and `dispatch` types are predefined.
713+
*
714+
* This allows you to set the `state` and `dispatch` types once,
715+
* eliminating the need to specify them with every
716+
* {@linkcode ListenerMiddlewareInstance.stopListening stopListening} call.
717+
*
718+
* @returns A pre-typed `stopListening` with the state and dispatch types already defined.
719+
*
720+
* @example
721+
* ```ts
722+
* import { createListenerMiddleware } from '@reduxjs/toolkit'
723+
*
724+
* const listenerMiddleware = createListenerMiddleware()
725+
*
726+
* export const stopAppListening = listenerMiddleware.stopListening.withTypes<
727+
* RootState,
728+
* AppDispatch
729+
* >()
730+
* ```
731+
*
732+
* @template OverrideStateType - The specific type of state the middleware listener operates on.
733+
* @template OverrideDispatchType - The specific type of the dispatch function.
734+
*
735+
* @since 2.1.0
736+
*/
617737
withTypes: <
618738
OverrideStateType extends StateType,
619-
OverrideDispatchType extends DispatchType
739+
OverrideDispatchType extends ReduxDispatch = ThunkDispatch<
740+
OverrideStateType,
741+
unknown,
742+
UnknownAction
743+
>
620744
>() => TypedStopListening<OverrideStateType, OverrideDispatchType>
621745
}
622746

623-
/** @public
624-
* A "pre-typed" version of `createListenerEntry`, so the listener args are well-typed */
747+
/**
748+
* A "pre-typed" version of `createListenerEntry`, so the listener args are well-typed
749+
*
750+
* @public
751+
*/
625752
export type TypedCreateListenerEntry<
626753
StateType,
627754
DispatchType extends ReduxDispatch = ThunkDispatch<
@@ -634,9 +761,37 @@ export type TypedCreateListenerEntry<
634761
StateType,
635762
DispatchType
636763
> & {
764+
/**
765+
* Creates a "pre-typed" version of `createListenerEntry`
766+
* where the `state` and `dispatch` types are predefined.
767+
*
768+
* This allows you to set the `state` and `dispatch` types once, eliminating
769+
* the need to specify them with every `createListenerEntry` call.
770+
*
771+
* @returns A pre-typed `createListenerEntry` with the state and dispatch types already defined.
772+
*
773+
* @example
774+
* ```ts
775+
* import { createListenerEntry } from '@reduxjs/toolkit'
776+
*
777+
* export const createAppListenerEntry = createListenerEntry.withTypes<
778+
* RootState,
779+
* AppDispatch
780+
* >()
781+
* ```
782+
*
783+
* @template OverrideStateType - The specific type of state the middleware listener operates on.
784+
* @template OverrideDispatchType - The specific type of the dispatch function.
785+
*
786+
* @since 2.1.0
787+
*/
637788
withTypes: <
638789
OverrideStateType extends StateType,
639-
OverrideDispatchType extends DispatchType
790+
OverrideDispatchType extends ReduxDispatch = ThunkDispatch<
791+
OverrideStateType,
792+
unknown,
793+
UnknownAction
794+
>
640795
>() => TypedStopListening<OverrideStateType, OverrideDispatchType>
641796
}
642797

packages/toolkit/src/tests/tsconfig.typetests.json

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,5 @@
44
"skipLibCheck": true,
55
"rootDir": "../../src"
66
},
7-
"include": [
8-
// "../../src/**/*.ts*",
9-
"../dynamicMiddleware",
10-
"../entities",
11-
"../listenerMiddleware"
12-
]
7+
"include": ["../dynamicMiddleware", "../entities", "../listenerMiddleware"]
138
}

0 commit comments

Comments
 (0)