thunkAPI.getState 方法无法正确推断状态类型

sli*_*wp2 9 typescript redux-toolkit

代码在这里:

users.slice.ts:

import { createAsyncThunk, createSlice } from '@reduxjs/toolkit';

const userAPI = {
  async fetchById(userId: string) {
    return { data: { id: userId, name: 'teresa teng' } };
  },
};

export const fetchUserById = createAsyncThunk<User, string>(
  'users/fetchByIdStatus',
  async (userId, thunkAPI) => {
    const response = await userAPI.fetchById(userId);
    return response.data;
  },
  {
    condition: (userId, { getState }) => {
      const { users } = getState();
      const { fetchStatus } = users[userId];
      if (fetchStatus === 'loading') {
        return false;
      }
    },
  }
);

interface User {
  name: string;
  id: string;
}

interface UsersSliceState {
  [id: string]: { fetchStatus: 'idle' | 'loading'; data: User };
}

const usersSliceState: UsersSliceState = {
  1: {
    fetchStatus: 'idle',
    data: { name: '', id: '' },
  },
};

const usersSlice = createSlice({
  name: 'users',
  initialState: usersSliceState,
  reducers: {},
  extraReducers: (builder) => {
    builder.addCase(fetchUserById.fulfilled, (state, action) => {
      console.log(action);
    });
  },
});

export default usersSlice.reducer;
Run Code Online (Sandbox Code Playgroud)

store.ts:

import { configureStore } from '@reduxjs/toolkit';
import usersSliceReducer from './users.slice';

export const store = configureStore({
  reducer: {
    users: usersSliceReducer,
  },
});

export type RootState = ReturnType<typeof store.getState>;
Run Code Online (Sandbox Code Playgroud)

main.ts:

import { store } from './store';
import { fetchUserById } from './users.slice';

store.dispatch(fetchUserById('1'));
Run Code Online (Sandbox Code Playgroud)

getState()当我调用方法并解构函数users内部的状态切片时,TSC 抛出错误condition

类型“{}”上不存在属性“users”。ts(2339)

推断的类型RootState是:

type RootState = {
    users: UsersSliceState;
}
Run Code Online (Sandbox Code Playgroud)

我应该如何正确地制作这种类型?如果我导入RootState类型并对返回值进行类型转换getState(),会导致循环引用吗?

软件包版本:

import { createAsyncThunk, createSlice } from '@reduxjs/toolkit';

const userAPI = {
  async fetchById(userId: string) {
    return { data: { id: userId, name: 'teresa teng' } };
  },
};

export const fetchUserById = createAsyncThunk<User, string>(
  'users/fetchByIdStatus',
  async (userId, thunkAPI) => {
    const response = await userAPI.fetchById(userId);
    return response.data;
  },
  {
    condition: (userId, { getState }) => {
      const { users } = getState();
      const { fetchStatus } = users[userId];
      if (fetchStatus === 'loading') {
        return false;
      }
    },
  }
);

interface User {
  name: string;
  id: string;
}

interface UsersSliceState {
  [id: string]: { fetchStatus: 'idle' | 'loading'; data: User };
}

const usersSliceState: UsersSliceState = {
  1: {
    fetchStatus: 'idle',
    data: { name: '', id: '' },
  },
};

const usersSlice = createSlice({
  name: 'users',
  initialState: usersSliceState,
  reducers: {},
  extraReducers: (builder) => {
    builder.addCase(fetchUserById.fulfilled, (state, action) => {
      console.log(action);
    });
  },
});

export default usersSlice.reducer;
Run Code Online (Sandbox Code Playgroud)

yud*_*esh 6

您需要将createAsyncThunkstate中的指定为 be ,其中是用于定义字段类型的可选字段的一部分。RootStatestatethunkApi

export const fetchUserById = createAsyncThunk<
 User,
 string, 
 { state: RootState}
>(
  'users/fetchByIdStatus',
  async (userId, thunkAPI) => {
    const response = await userAPI.fetchById(userId);
    return response.data;
  },
  {
    condition: (userId, { getState }) => {
      const { users } = getState();
      const { fetchStatus } = users[userId];
      if (fetchStatus === 'loading') {
        return false;
      }
    },
  }
);
Run Code Online (Sandbox Code Playgroud)