Mohammad Rafi
Mohammad Rafi

Reputation: 21

How can I skip first argument to the payload creator of Redux Thunk using typescript

const fetchUserById = createAsyncThunk<
  // Return type of the payload creator
  MyData,
  // First argument to the payload creator
  number,
  {
    // Optional fields for defining thunkApi field types
    dispatch: AppDispatch
    state: State
    extra: {
      jwt: string
    }
  }
>('users/fetchById', async (userId, thunkApi) => {
  const response = await fetch(`https://reqres.in/api/users/${userId}`, {
    headers: {
      Authorization: `Bearer ${thunkApi.extra.jwt}`,
    },
  })
  return (await response.json()) as MyData
})

This snippet is from redux-tooklit documentation. Here I am going to use get request, so I won't have arguments passed. How can I skip the first argument to the payload creator userId

Upvotes: 0

Views: 281

Answers (1)

Mohammad Rafi
Mohammad Rafi

Reputation: 21

so to avoid passing any unnecessary arguments we can simply just do it like this:

const fetchUserById = createAsyncThunk<
  // Return type of the payload creator
  MyData,
  // First argument to the payload creator
  void, // pass void type here
  {
    // Optional fields for defining thunkApi field types
    dispatch: AppDispatch
    state: State
    extra: {
      jwt: string
    }
  }
>('users/fetchById', async (_, thunkApi) => { // pass underscore(_) here
  const response = await fetch(`https://reqres.in/api/users/${userId}`, {
    headers: {
      Authorization: `Bearer ${thunkApi.extra.jwt}`,
    },
  })
  return (await response.json()) as MyData
})

Upvotes: 1

Related Questions