Skip to content

Commit 24fb2e6

Browse files
authored
docs(toolkit): resolve minor JSDoc issues (#5227)
* docs(toolkit): resolve minor JSDoc issues * docs(toolkit): resolve minor JSDoc issues * docs(toolkit): resolve minor JSDoc issues
1 parent 27be012 commit 24fb2e6

File tree

9 files changed

+624
-492
lines changed

9 files changed

+624
-492
lines changed

errors.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,4 @@
4343
"41": "getPreviousPageParam for endpoint '' must be a function if maxPages is used",
4444
"42": "Duplicate middleware references found when creating the store. Ensure that each middleware is only included once.",
4545
"43": "`builder.addAsyncThunk` should only be called before calling `builder.addDefaultCase`"
46-
}
46+
}

packages/toolkit/src/actionCreatorInvariantMiddleware.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export function getMessage(type?: unknown) {
1414
const actionName = splitType[splitType.length - 1] || 'actionCreator'
1515
return `Detected an action creator with type "${
1616
type || 'unknown'
17-
}" being dispatched.
17+
}" being dispatched.
1818
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.`
1919
}
2020

packages/toolkit/src/createReducer.ts

Lines changed: 36 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -100,46 +100,42 @@ export type ReducerWithInitialState<S extends NotFunction<any>> = Reducer<S> & {
100100
* @param builderCallback - `(builder: Builder) => void` A callback that receives a *builder* object to define
101101
* case reducers via calls to `builder.addCase(actionCreatorOrType, reducer)`.
102102
* @example
103-
```ts
104-
import {
105-
createAction,
106-
createReducer,
107-
UnknownAction,
108-
PayloadAction,
109-
} from "@reduxjs/toolkit";
110-
111-
const increment = createAction<number>("increment");
112-
const decrement = createAction<number>("decrement");
113-
114-
function isActionWithNumberPayload(
115-
action: UnknownAction
116-
): action is PayloadAction<number> {
117-
return typeof action.payload === "number";
118-
}
119-
120-
const reducer = createReducer(
121-
{
122-
counter: 0,
123-
sumOfNumberPayloads: 0,
124-
unhandledActions: 0,
125-
},
126-
(builder) => {
127-
builder
128-
.addCase(increment, (state, action) => {
129-
// action is inferred correctly here
130-
state.counter += action.payload;
131-
})
132-
// You can chain calls, or have separate `builder.addCase()` lines each time
133-
.addCase(decrement, (state, action) => {
134-
state.counter -= action.payload;
135-
})
136-
// You can apply a "matcher function" to incoming actions
137-
.addMatcher(isActionWithNumberPayload, (state, action) => {})
138-
// and provide a default case if no other handlers matched
139-
.addDefaultCase((state, action) => {});
140-
}
141-
);
142-
```
103+
* ```ts
104+
* import type { PayloadAction, UnknownAction } from '@reduxjs/toolkit';
105+
* import { createAction, createReducer } from '@reduxjs/toolkit';
106+
*
107+
* const increment = createAction<number>('increment');
108+
* const decrement = createAction<number>('decrement');
109+
*
110+
* function isActionWithNumberPayload(
111+
* action: UnknownAction,
112+
* ): action is PayloadAction<number> {
113+
* return typeof action.payload === 'number';
114+
* }
115+
*
116+
* const reducer = createReducer(
117+
* {
118+
* counter: 0,
119+
* sumOfNumberPayloads: 0,
120+
* unhandledActions: 0,
121+
* },
122+
* (builder) => {
123+
* builder
124+
* .addCase(increment, (state, action) => {
125+
* // action is inferred correctly here
126+
* state.counter += action.payload;
127+
* })
128+
* // You can chain calls, or have separate `builder.addCase()` lines each time
129+
* .addCase(decrement, (state, action) => {
130+
* state.counter -= action.payload;
131+
* })
132+
* // You can apply a "matcher function" to incoming actions
133+
* .addMatcher(isActionWithNumberPayload, (state, action) => {})
134+
* // and provide a default case if no other handlers matched
135+
* .addDefaultCase((state, action) => {});
136+
* },
137+
* );
138+
* ```
143139
* @public
144140
*/
145141
export function createReducer<S extends NotFunction<any>>(

packages/toolkit/src/createSlice.ts

Lines changed: 48 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -224,41 +224,43 @@ export interface CreateSliceOptions<
224224
*
225225
*
226226
* @example
227-
```ts
228-
import { createAction, createSlice, Action } from '@reduxjs/toolkit'
229-
const incrementBy = createAction<number>('incrementBy')
230-
const decrement = createAction('decrement')
231-
232-
interface RejectedAction extends Action {
233-
error: Error
234-
}
235-
236-
function isRejectedAction(action: Action): action is RejectedAction {
237-
return action.type.endsWith('rejected')
238-
}
239-
240-
createSlice({
241-
name: 'counter',
242-
initialState: 0,
243-
reducers: {},
244-
extraReducers: builder => {
245-
builder
246-
.addCase(incrementBy, (state, action) => {
247-
// action is inferred correctly here if using TS
248-
})
249-
// You can chain calls, or have separate `builder.addCase()` lines each time
250-
.addCase(decrement, (state, action) => {})
251-
// You can match a range of action types
252-
.addMatcher(
253-
isRejectedAction,
254-
// `action` will be inferred as a RejectedAction due to isRejectedAction being defined as a type guard
255-
(state, action) => {}
256-
)
257-
// and provide a default case if no other handlers matched
258-
.addDefaultCase((state, action) => {})
259-
}
260-
})
261-
```
227+
* ```ts
228+
* import type { Action } from '@reduxjs/toolkit';
229+
* import { createAction, createSlice } from '@reduxjs/toolkit';
230+
*
231+
* const incrementBy = createAction<number>('incrementBy');
232+
* const decrement = createAction('decrement');
233+
*
234+
* interface RejectedAction extends Action {
235+
* error: Error;
236+
* }
237+
*
238+
* function isRejectedAction(action: Action): action is RejectedAction {
239+
* return action.type.endsWith('rejected');
240+
* }
241+
*
242+
* createSlice({
243+
* name: 'counter',
244+
* initialState: 0,
245+
* reducers: {},
246+
* extraReducers: (builder) => {
247+
* builder
248+
* .addCase(incrementBy, (state, action) => {
249+
* // action is inferred correctly here if using TS
250+
* })
251+
* // You can chain calls, or have separate `builder.addCase()` lines each time
252+
* .addCase(decrement, (state, action) => {})
253+
* // You can match a range of action types
254+
* .addMatcher(
255+
* isRejectedAction,
256+
* // `action` will be inferred as a RejectedAction due to isRejectedAction being defined as a type guard
257+
* (state, action) => {},
258+
* )
259+
* // and provide a default case if no other handlers matched
260+
* .addDefaultCase((state, action) => {});
261+
* },
262+
* });
263+
* ```
262264
*/
263265
extraReducers?: (builder: ActionReducerMapBuilder<State>) => void
264266

@@ -905,11 +907,13 @@ interface ReducerHandlingContextMethods<State> {
905907
* @param name The key to be exposed as.
906908
* @param actionCreator The action to expose.
907909
* @example
908-
* context.exposeAction("addPost", createAction<Post>("addPost"));
910+
* ```ts
911+
* context.exposeAction('addPost', createAction<Post>('addPost'));
909912
*
910-
* export const { addPost } = slice.actions
913+
* export const { addPost } = slice.actions;
911914
*
912-
* dispatch(addPost(post))
915+
* dispatch(addPost(post));
916+
* ```
913917
*/
914918
exposeAction(
915919
name: string,
@@ -920,11 +924,13 @@ interface ReducerHandlingContextMethods<State> {
920924
* @param name The key to be exposed as.
921925
* @param reducer The reducer to expose.
922926
* @example
923-
* context.exposeCaseReducer("addPost", (state, action: PayloadAction<Post>) => {
924-
* state.push(action.payload)
925-
* })
927+
* ```ts
928+
* context.exposeCaseReducer('addPost', (state, action: PayloadAction<Post>) => {
929+
* state.push(action.payload);
930+
* });
926931
*
927-
* slice.caseReducers.addPost([], addPost(post))
932+
* slice.caseReducers.addPost([], addPost(post));
933+
* ```
928934
*/
929935
exposeCaseReducer(
930936
name: string,

packages/toolkit/src/listenerMiddleware/types.ts

Lines changed: 45 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -110,11 +110,11 @@ export interface ForkedTask<T> {
110110
*
111111
* ### Example
112112
* ```ts
113-
* const result = await fork(async (forkApi) => Promise.resolve(4)).result
113+
* const result = await fork(async (forkApi) => Promise.resolve(4)).result;
114114
*
115-
* if(result.status === 'ok') {
116-
* console.log(result.value) // logs 4
117-
* }}
115+
* if (result.status === 'ok') {
116+
* console.log(result.value); // logs 4
117+
* }
118118
* ```
119119
*/
120120
result: Promise<TaskResult<T>>
@@ -151,17 +151,17 @@ export interface ListenerEffectAPI<
151151
*
152152
* ```ts
153153
* middleware.startListening({
154-
* predicate: () => true,
155-
* async effect(_, { getOriginalState }) {
156-
* getOriginalState(); // sync: OK!
154+
* predicate: () => true,
155+
* async effect(_, { getOriginalState }) {
156+
* getOriginalState(); // sync: OK!
157157
*
158-
* setTimeout(getOriginalState, 0); // async: throws Error
158+
* setTimeout(getOriginalState, 0); // async: throws Error
159159
*
160-
* await Promise().resolve();
160+
* await Promise().resolve();
161161
*
162-
* getOriginalState() // async: throws Error
163-
* }
164-
* })
162+
* getOriginalState(); // async: throws Error
163+
* },
164+
* });
165165
* ```
166166
*/
167167
getOriginalState: () => State
@@ -184,17 +184,19 @@ export interface ListenerEffectAPI<
184184
* ### Example
185185
*
186186
* ```ts
187+
* import { createAction } from '@reduxjs/toolkit';
188+
*
187189
* const updateBy = createAction<number>('counter/updateBy');
188190
*
189191
* middleware.startListening({
190-
* actionCreator: updateBy,
191-
* async effect(_, { condition }) {
192-
* // wait at most 3s for `updateBy` actions.
193-
* if(await condition(updateBy.match, 3_000)) {
194-
* // `updateBy` has been dispatched twice in less than 3s.
195-
* }
196-
* }
197-
* })
192+
* actionCreator: updateBy,
193+
* async effect(_, { condition }) {
194+
* // wait at most 3s for `updateBy` actions.
195+
* if (await condition(updateBy.match, 3_000)) {
196+
* // `updateBy` has been dispatched twice in less than 3s.
197+
* }
198+
* },
199+
* });
198200
* ```
199201
*/
200202
condition: ConditionFunction<State>
@@ -273,8 +275,8 @@ export type ListenerEffect<
273275
) => void | Promise<void>
274276

275277
/**
276-
* @public
277278
* Additional infos regarding the error raised.
279+
* @public
278280
*/
279281
export interface ListenerErrorInfo {
280282
/**
@@ -284,10 +286,10 @@ export interface ListenerErrorInfo {
284286
}
285287

286288
/**
287-
* @public
288289
* Gets notified with synchronous and asynchronous errors raised by `listeners` or `predicates`.
289290
* @param error The thrown error.
290291
* @param errorInfo Additional information regarding the thrown error.
292+
* @public
291293
*/
292294
export interface ListenerErrorHandler {
293295
(error: unknown, errorInfo: ListenerErrorInfo): void
@@ -396,8 +398,11 @@ export type UnsubscribeListener = (
396398
) => void
397399

398400
/**
401+
* The possible overloads and options for defining a listener.
402+
* The return type of each function is specified as a generic arg,
403+
* so the overloads can be reused for multiple different functions.
404+
*
399405
* @public
400-
* The possible overloads and options for defining a listener. The return type of each function is specified as a generic arg, so the overloads can be reused for multiple different functions
401406
*/
402407
export type AddListenerOverloads<
403408
Return,
@@ -555,9 +560,13 @@ export type TypedAddListener<
555560
*
556561
* @example
557562
* ```ts
558-
* import { addListener } from '@reduxjs/toolkit'
563+
* import { addListener } from '@reduxjs/toolkit';
559564
*
560-
* export const addAppListener = addListener.withTypes<RootState, AppDispatch, ExtraArguments>()
565+
* export const addAppListener = addListener.withTypes<
566+
* RootState,
567+
* AppDispatch,
568+
* ExtraArguments
569+
* >();
561570
* ```
562571
*
563572
* @template OverrideStateType - The specific type of state the middleware listener operates on.
@@ -667,12 +676,13 @@ export type TypedStartListening<
667676
> & {
668677
/**
669678
* Creates a "pre-typed" version of
670-
* {@linkcode ListenerMiddlewareInstance.startListening startListening}
679+
* {@linkcode ListenerMiddlewareInstance.startListening | startListening}
671680
* where the `state`, `dispatch` and `extra` types are predefined.
672681
*
673682
* This allows you to set the `state`, `dispatch` and `extra` types once,
674683
* eliminating the need to specify them with every
675-
* {@linkcode ListenerMiddlewareInstance.startListening startListening} call.
684+
* {@linkcode ListenerMiddlewareInstance.startListening | startListening}
685+
* call.
676686
*
677687
* @returns A pre-typed `startListening` with the state, dispatch and extra types already defined.
678688
*
@@ -726,12 +736,12 @@ export type TypedStopListening<
726736
> = RemoveListenerOverloads<StateType, DispatchType, ExtraArgument> & {
727737
/**
728738
* Creates a "pre-typed" version of
729-
* {@linkcode ListenerMiddlewareInstance.stopListening stopListening}
739+
* {@linkcode ListenerMiddlewareInstance.stopListening | stopListening}
730740
* where the `state`, `dispatch` and `extra` types are predefined.
731741
*
732742
* This allows you to set the `state`, `dispatch` and `extra` types once,
733743
* eliminating the need to specify them with every
734-
* {@linkcode ListenerMiddlewareInstance.stopListening stopListening} call.
744+
* {@linkcode ListenerMiddlewareInstance.stopListening | stopListening} call.
735745
*
736746
* @returns A pre-typed `stopListening` with the state, dispatch and extra types already defined.
737747
*
@@ -834,7 +844,11 @@ export type TypedCreateListenerEntry<
834844
* Internal Types
835845
*/
836846

837-
/** @internal An single listener entry */
847+
/**
848+
* An single listener entry
849+
*
850+
* @internal
851+
*/
838852
export type ListenerEntry<
839853
State = unknown,
840854
DispatchType extends Dispatch = Dispatch,
@@ -848,8 +862,8 @@ export type ListenerEntry<
848862
}
849863

850864
/**
851-
* @internal
852865
* A shorthand form of the accepted args, solely so that `createListenerEntry` has validly-typed conditional logic when checking the options contents
866+
* @internal
853867
*/
854868
export type FallbackAddListenerOptions = {
855869
actionCreator?: TypedActionCreatorWithMatchFunction<string>

0 commit comments

Comments
 (0)