如何在typescript中访问redux工具包中的getState?

new9mtju  于 2022-12-05  发布在  TypeScript
关注(0)|答案(1)|浏览(139)

我有一个createAsyncThunk操作,想访问工具包中的getState。我不确定如何在typescript中转换getState类型:

export const getInitialDatapoints = createAsyncThunk(
  'userSlice/getInitDatapoints',
  async (args, { getState as AppState }) => {
    const geoData = await getLocationData();
    const googleData = getState().userSlice;
    const response = await updateUserData(1);
    return response.data;
  }
);

其中AppStore为:

export type AppState = ReturnType<typeof store.getState>;

我得到这个错误:

Property 'AppState' does not exist on type 'GetThunkAPI<{}>'

有人能帮助解决这个问题吗?

toiithl6

toiithl61#

解构对象时不能Assert类型。另外,在将getStateAssert为AppState之前,需要调用getState
您可以改为执行以下操作:

const getInitialDatapoints = createAsyncThunk(
        "userSlice/getInitDatapoints",
        async (args, api) => {
            const appState = api.getState() as AppState
            const geoData = await getLocationData();
            const googleData = appState.userSlice;
            const response = await updateUserData(1);
            return response.data;
        }
    );

相关问题