Repository: reduxjs/redux-toolkit (ts)

Issue:
Return 'type' prop in createAsyncThunk
I'm implementing a loading/error reducer to manage async states and I think it would be a good idea to return type property. This way I could use selectors like:

`// thunk`
`const login = createAsyncThunk('login', () => {})`
`// login.type --> 'login'`

`// selector`
`const isLoading = useSelector(selectLoading([login.type]));` 

Pull request:
expose createAsyncThunk typePrefix parameter on the actionCreator
this should resolve #525

Patch:
diff --git a/etc/redux-toolkit.api.md b/etc/redux-toolkit.api.md
index 9b88eec2..401db656 100644
--- a/etc/redux-toolkit.api.md
+++ b/etc/redux-toolkit.api.md
@@ -124,7 +124,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>>, options?: AsyncThunkOptions<ThunkArg, ThunkApiConfig>): IsAny<ThunkArg, (arg: ThunkArg) => AsyncThunkAction<Returned, ThunkArg, ThunkApiConfig>, unknown extends ThunkArg ? (arg: ThunkArg) => AsyncThunkAction<Returned, ThunkArg, ThunkApiConfig> : [ThunkArg] extends [void] | [undefined] ? () => AsyncThunkAction<Returned, ThunkArg, ThunkApiConfig> : [void] extends [ThunkArg] ? (arg?: ThunkArg | undefined) => AsyncThunkAction<Returned, ThunkArg, ThunkApiConfig> : [undefined] extends [ThunkArg] ? (arg?: ThunkArg | undefined) => AsyncThunkAction<Returned, ThunkArg, ThunkApiConfig> : (arg: ThunkArg) => AsyncThunkAction<Returned, ThunkArg, ThunkApiConfig>> & {
+export function createAsyncThunk<Returned, ThunkArg = void, ThunkApiConfig extends AsyncThunkConfig = {}>(typePrefix: string, payloadCreator: (arg: ThunkArg, thunkAPI: GetThunkAPI<ThunkApiConfig>) => Promise<Returned | RejectWithValue<GetRejectValue<ThunkApiConfig>>> | Returned | RejectWithValue<GetRejectValue<ThunkApiConfig>>, options?: AsyncThunkOptions<ThunkArg, ThunkApiConfig>): IsAny<ThunkArg, (arg: ThunkArg) => AsyncThunkAction<Returned, ThunkArg, ThunkApiConfig>, unknown extends ThunkArg ? (arg: ThunkArg) => AsyncThunkAction<Returned, ThunkArg, ThunkApiConfig> : [ThunkArg] extends [void] | [undefined] ? () => AsyncThunkAction<Returned, ThunkArg, ThunkApiConfig> : [void] extends [ThunkArg] ? (arg?: ThunkArg | undefined) => AsyncThunkAction<Returned, ThunkArg, ThunkApiConfig> : [undefined] extends [ThunkArg] ? (arg?: ThunkArg | undefined) => AsyncThunkAction<Returned, ThunkArg, ThunkApiConfig> : (arg: ThunkArg) => AsyncThunkAction<Returned, ThunkArg, ThunkApiConfig>> & {
     pending: ActionCreatorWithPreparedPayload<[string, ThunkArg], undefined, string, never, {
         arg: ThunkArg;
         requestId: string;
@@ -139,6 +139,7 @@ export function createAsyncThunk<Returned, ThunkArg = void, ThunkApiConfig exten
         arg: ThunkArg;
         requestId: string;
     }>;
+    typePrefix: string;
 };
 
 // @public (undocumented)
diff --git a/src/createAsyncThunk.ts b/src/createAsyncThunk.ts
index ea0cc79c..8e6b3e2d 100644
--- a/src/createAsyncThunk.ts
+++ b/src/createAsyncThunk.ts
@@ -217,7 +217,7 @@ interface AsyncThunkOptions<
 
 /**
  *
- * @param type
+ * @param typePrefix
  * @param payloadCreator
  * @param options
  *
@@ -228,7 +228,7 @@ export function createAsyncThunk<
   ThunkArg = void,
   ThunkApiConfig extends AsyncThunkConfig = {}
 >(
-  type: string,
+  typePrefix: string,
   payloadCreator: (
     arg: ThunkArg,
     thunkAPI: GetThunkAPI<ThunkApiConfig>
@@ -241,7 +241,7 @@ export function createAsyncThunk<
   type RejectedValue = GetRejectValue<ThunkApiConfig>
 
   const fulfilled = createAction(
-    type + '/fulfilled',
+    typePrefix + '/fulfilled',
     (result: Returned, requestId: string, arg: ThunkArg) => {
       return {
         payload: result,
@@ -251,7 +251,7 @@ export function createAsyncThunk<
   )
 
   const pending = createAction(
-    type + '/pending',
+    typePrefix + '/pending',
     (requestId: string, arg: ThunkArg) => {
       return {
         payload: undefined,
@@ -261,7 +261,7 @@ export function createAsyncThunk<
   )
 
   const rejected = createAction(
-    type + '/rejected',
+    typePrefix + '/rejected',
     (
       error: Error | null,
       requestId: string,
@@ -397,7 +397,8 @@ If you want to use the AbortController to react to \`abort\` events, please cons
     {
       pending,
       rejected,
-      fulfilled
+      fulfilled,
+      typePrefix
     }
   )
 }


Test patch:
diff --git a/src/createAsyncThunk.test.ts b/src/createAsyncThunk.test.ts
index 38644b83..49969318 100644
--- a/src/createAsyncThunk.test.ts
+++ b/src/createAsyncThunk.test.ts
@@ -27,6 +27,12 @@ describe('createAsyncThunk', () => {
     expect(thunkActionCreator.rejected.type).toBe('testType/rejected')
   })
 
+  it('exposes the typePrefix it was created with', () => {
+    const thunkActionCreator = createAsyncThunk('testType', async () => 42)
+
+    expect(thunkActionCreator.typePrefix).toBe('testType')
+  })
+
   it('works without passing arguments to the payload creator', async () => {
     const thunkActionCreator = createAsyncThunk('testType', async () => 42)