Repository: reduxjs/redux-toolkit
Pull request #512 (2020-04-18 21:25:33)

export AsyncThunkPayloadCreator type
This should solve #486 

netlify[bot]: Deploy preview for *redux-starter-kit-docs* ready!

Built with commit a27f8770fab5e8da4ec1957181eca6c035c11f7d

https://deploy-preview-512--redux-starter-kit-docs.netlify.app
codesandbox[bot]: This pull request is automatically built and testable in [CodeSandbox](https://codesandbox.io).

  To see build info of the built libraries, click [here](https://ci.codesandbox.io/status/reduxjs/redux-toolkit/pr/512/builds/20732) or the icon next to each commit SHA.

Latest deployment of this branch, based on commit a27f8770fab5e8da4ec1957181eca6c035c11f7d:

|Sandbox| Source |
|--|--|
|[lively-brook-9h028](https://codesandbox.io/s/lively-brook-9h028)| Configuration |
|[musing-wilbur-33q4z](https://codesandbox.io/s/musing-wilbur-33q4z)| Configuration |
|[hidden-hill-prnr9](https://codesandbox.io/s/hidden-hill-prnr9)| Configuration |


Patch:
diff --git a/docs/api/getDefaultMiddleware.md b/docs/api/getDefaultMiddleware.md
index ac51a106..bc9928a3 100644
--- a/docs/api/getDefaultMiddleware.md
+++ b/docs/api/getDefaultMiddleware.md
@@ -110,7 +110,8 @@ interface ThunkOptions<E = any> {
 
 interface ImmutableStateInvariantMiddlewareOptions {
   isImmutable?: (value: any) => boolean
-  ignore?: string[]
+  ignoredPaths?: string[]
+  warnAfter?: number
 }
 
 interface SerializableStateInvariantMiddlewareOptions {
@@ -132,10 +133,22 @@ interface SerializableStateInvariantMiddlewareOptions {
    */
   ignoredActions?: string[]
 
+  /**
+   * An array of dot-separated path strings to ignore when checking for serializability, Defaults to ['meta.arg']
+   * If you use this parameter, the default value 'meta.arg' will be removed, so we recommend re-adding it unless you
+   * specifically do not want to ignore it. Example: ['meta.arg', 'your.path', 'other.path', ...etc]
+   */
+  ignoredActionPaths?: string[]
+
   /**
    * An array of dot-separated path strings to ignore when checking for serializability, Defaults to []
    */
   ignoredPaths?: string[]
+
+  /**
+   * Execution time warning threshold. If the middleware takes longer than `warnAfter` ms, a warning will be displayed in the console. Defaults to 32
+   */
+  warnAfter?: number
 }
 
 interface GetDefaultMiddlewareOptions {
diff --git a/docs/api/otherExports.md b/docs/api/otherExports.md
index 4a5b5af9..8ef68663 100644
--- a/docs/api/otherExports.md
+++ b/docs/api/otherExports.md
@@ -80,9 +80,15 @@ function createSerializableStateInvariantMiddleware({
   // An array of action types to ignore when checking for serializability.
   // Defaults to []
   ignoredActions?: string[]
-  // An array of dot-separated path strings to ignore when checking for serializability.
-  // Defaults to []
+  // An array of dot-separated path strings to ignore when
+  // checking for serializability, Defaults to ['meta.arg']
+  ignoredActionPaths?: string[]
+  // An array of dot-separated path strings to ignore when
+  // checking for serializability, Defaults to []
   ignoredPaths?: string[]
+  // Execution time warning threshold. If the middleware takes longer
+  // than `warnAfter` ms, a warning will be displayed in the console. Defaults to 32
+  warnAfter?: number
 })
 ```
 
diff --git a/etc/redux-toolkit.api.md b/etc/redux-toolkit.api.md
index ee7be645..623fa7e1 100644
--- a/etc/redux-toolkit.api.md
+++ b/etc/redux-toolkit.api.md
@@ -59,6 +59,12 @@ export interface ActionReducerMapBuilder<State> {
 // @public @deprecated
 export type Actions<T extends keyof any = string> = Record<T, Action>;
 
+// @public
+export type AsyncThunkPayloadCreator<Returned, ThunkArg = void, ThunkApiConfig extends AsyncThunkConfig = {}> = (arg: ThunkArg, thunkAPI: GetThunkAPI<ThunkApiConfig>) => AsyncThunkPayloadCreatorReturnValue<Returned, ThunkApiConfig>;
+
+// @public
+export type AsyncThunkPayloadCreatorReturnValue<Returned, ThunkApiConfig extends AsyncThunkConfig> = Promise<Returned | RejectWithValue<GetRejectValue<ThunkApiConfig>>> | Returned | RejectWithValue<GetRejectValue<ThunkApiConfig>>;
+
 // @public
 export type CaseReducer<S = any, A extends Action = AnyAction> = (state: Draft<S>, action: A) => S | void;
 
@@ -105,7 +111,7 @@ export function createAction<P = void, T extends string = string>(type: T): Payl
 export function createAction<PA extends PrepareAction<any>, T extends string = string>(type: T, prepareAction: PA): PayloadActionCreator<ReturnType<PA>['payload'], T, PA>;
 
 // @public (undocumented)
-export function createAsyncThunk<Returned, ThunkArg = void, ThunkApiConfig extends AsyncThunkConfig = {}>(type: string, payloadCreator: (arg: ThunkArg, thunkAPI: GetThunkAPI<ThunkApiConfig>) => Promise<Returned | RejectWithValue<GetRejectValue<ThunkApiConfig>>> | Returned | RejectWithValue<GetRejectValue<ThunkApiConfig>>): ((arg: ThunkArg) => (dispatch: GetDispatch<ThunkApiConfig>, getState: () => GetState<ThunkApiConfig>, extra: GetExtra<ThunkApiConfig>) => Promise<PayloadAction<Returned, string, {
+export function createAsyncThunk<Returned, ThunkArg = void, ThunkApiConfig extends AsyncThunkConfig = {}>(type: string, payloadCreator: AsyncThunkPayloadCreator<Returned, ThunkArg, ThunkApiConfig>): ((arg: ThunkArg) => (dispatch: GetDispatch<ThunkApiConfig>, getState: () => GetState<ThunkApiConfig>, extra: GetExtra<ThunkApiConfig>) => Promise<PayloadAction<Returned, string, {
     arg: ThunkArg;
     requestId: string;
 }, never> | PayloadAction<GetRejectValue<ThunkApiConfig> | undefined, string, {
@@ -277,6 +283,8 @@ export type IdSelector<T> = (model: T) => EntityId;
 
 // @public
 export interface ImmutableStateInvariantMiddlewareOptions {
+    // (undocumented)
+    ignore?: string[];
     // (undocumented)
     ignoredPaths?: string[];
     // (undocumented)
diff --git a/src/createAsyncThunk.ts b/src/createAsyncThunk.ts
index 42a6e100..facec30e 100644
--- a/src/createAsyncThunk.ts
+++ b/src/createAsyncThunk.ts
@@ -102,6 +102,33 @@ type GetRejectValue<ThunkApiConfig> = ThunkApiConfig extends {
 }
   ? RejectValue
   : unknown
+/**
+ * A type describing the return value of the `payloadCreator` argument to `createAsyncThunk`.
+ * Might be useful for wrapping `createAsyncThunk` in custom abstractions.
+ *
+ * @public
+ */
+export type AsyncThunkPayloadCreatorReturnValue<
+  Returned,
+  ThunkApiConfig extends AsyncThunkConfig
+> =
+  | Promise<Returned | RejectWithValue<GetRejectValue<ThunkApiConfig>>>
+  | Returned
+  | RejectWithValue<GetRejectValue<ThunkApiConfig>>
+/**
+ * A type describing the `payloadCreator` argument to `createAsyncThunk`.
+ * Might be useful for wrapping `createAsyncThunk` in custom abstractions.
+ *
+ * @public
+ */
+export type AsyncThunkPayloadCreator<
+  Returned,
+  ThunkArg = void,
+  ThunkApiConfig extends AsyncThunkConfig = {}
+> = (
+  arg: ThunkArg,
+  thunkAPI: GetThunkAPI<ThunkApiConfig>
+) => AsyncThunkPayloadCreatorReturnValue<Returned, ThunkApiConfig>
 
 /**
  *
@@ -116,13 +143,7 @@ export function createAsyncThunk<
   ThunkApiConfig extends AsyncThunkConfig = {}
 >(
   type: string,
-  payloadCreator: (
-    arg: ThunkArg,
-    thunkAPI: GetThunkAPI<ThunkApiConfig>
-  ) =>
-    | Promise<Returned | RejectWithValue<GetRejectValue<ThunkApiConfig>>>
-    | Returned
-    | RejectWithValue<GetRejectValue<ThunkApiConfig>>
+  payloadCreator: AsyncThunkPayloadCreator<Returned, ThunkArg, ThunkApiConfig>
 ) {
   type RejectedValue = GetRejectValue<ThunkApiConfig>
 
diff --git a/src/immutableStateInvariantMiddleware.ts b/src/immutableStateInvariantMiddleware.ts
index 2a0f1687..0455306b 100644
--- a/src/immutableStateInvariantMiddleware.ts
+++ b/src/immutableStateInvariantMiddleware.ts
@@ -187,6 +187,7 @@ export interface ImmutableStateInvariantMiddlewareOptions {
   isImmutable?: IsImmutableFunc
   ignoredPaths?: string[]
   warnAfter?: number
+  ignore?: string[] // @deprecated. Use ignoredPaths
 }
 
 /**
@@ -205,11 +206,16 @@ export function createImmutableStateInvariantMiddleware(
     return () => next => action => next(action)
   }
 
-  const {
+  let {
     isImmutable = isImmutableDefault,
     ignoredPaths,
-    warnAfter = 32
+    warnAfter = 32,
+    ignore
   } = options
+
+  // Alias ignore->ignoredPaths, but prefer ignoredPaths if present
+  ignoredPaths = ignoredPaths || ignore
+
   const track = trackForMutations.bind(null, isImmutable, ignoredPaths)
 
   return ({ getState }) => {
diff --git a/src/index.ts b/src/index.ts
index d4e1d849..3d1f1527 100644
--- a/src/index.ts
+++ b/src/index.ts
@@ -96,6 +96,8 @@ export {
 } from './entities/models'
 
 export {
+  AsyncThunkPayloadCreatorReturnValue,
+  AsyncThunkPayloadCreator,
   createAsyncThunk,
   unwrapResult,
   SerializedError


Test patch:
diff --git a/src/immutableStateInvariantMiddleware.test.ts b/src/immutableStateInvariantMiddleware.test.ts
index 104e677d..ab89d71e 100644
--- a/src/immutableStateInvariantMiddleware.test.ts
+++ b/src/immutableStateInvariantMiddleware.test.ts
@@ -110,7 +110,7 @@ describe('createImmutableStateInvariantMiddleware', () => {
     }).not.toThrow()
   })
 
-  it('respects "ignore" option', () => {
+  it('respects "ignoredPaths" option', () => {
     const next: Dispatch = action => {
       state.foo.bar.push(5)
       return action
@@ -123,6 +123,19 @@ describe('createImmutableStateInvariantMiddleware', () => {
     }).not.toThrow()
   })
 
+  it('alias "ignore" to "ignoredPath" and respects option', () => {
+    const next: Dispatch = action => {
+      state.foo.bar.push(5)
+      return action
+    }
+
+    const dispatch = middleware({ ignore: ['foo.bar'] })(next)
+
+    expect(() => {
+      dispatch({ type: 'SOME_ACTION' })
+    }).not.toThrow()
+  })
+
   it('Should print a warning if execution takes too long', () => {
     state.foo.bar = new Array(10000).fill({ value: 'more' })