将类型添加到 createAsyncThunk 中的异步函数

Adding types to async func inside createAsyncThunk

我在 createAsyncThunk 中创建异步函数时尝试为参数添加类型时遇到问题:

export const setLangAsync = createAsyncThunk<
  { lang: string },
  { fileName: string },
  { getState: RootState; dispatch: AppDispatch }
>(
  'i18n/setLangAsync',
  async (lang: string, fileName: string, { getState, dispatch }) => {
 ...
}

这里是错误:

(parameter) getState: any
Argument of type '(lang: string, fileName: string, { getState, dispatch }: 
{ getState: any; dispatch: any; }) => Promise<unknown>' 
is not assignable to parameter of type 'AsyncThunkPayloadCreator<
{ lang: string; }, { fileName: string; }, { getState: { i18n: I18nInterface; }; 
dispatch: ThunkDispatch<...> & Dispatch<...>; }>'.ts(2345)
Binding element 'getState' implicitly has an 'any' type.ts(7031

我好像完全迷失在这里了。我希望有人能帮助我理解我做错了什么。提前致谢!

createAsyncThunk 的 payload creator 函数有两个参数,而不是三个。

至于通用参数:

  • 第一个是return类型
  • 第二个是参数类型
  • 第三个是各种选项
export const setLangAsync = createAsyncThunk<
  WhatEverTheFunctionReturnTypeIs,
  { lang: string, fileName: string },
  { getState: RootState; dispatch: AppDispatch }
>(
  'i18n/setLangAsync',
  async ({ lang, fileName }, { getState, dispatch }) => {
 ...
}