Typescript thunkAPI.getState方法无法正确推断状态类型
代码如下:Typescript thunkAPI.getState方法无法正确推断状态类型,typescript,redux-toolkit,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 = createAsync
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;
import { configureStore } from '@reduxjs/toolkit';
import usersSliceReducer from './users.slice';
export const store = configureStore({
reducer: {
users: usersSliceReducer,
},
});
export type RootState = ReturnType<typeof store.getState>;
import { store } from './store';
import { fetchUserById } from './users.slice';
store.dispatch(fetchUserById('1'));
main.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;
import { configureStore } from '@reduxjs/toolkit';
import usersSliceReducer from './users.slice';
export const store = configureStore({
reducer: {
users: usersSliceReducer,
},
});
export type RootState = ReturnType<typeof store.getState>;
import { store } from './store';
import { fetchUserById } from './users.slice';
store.dispatch(fetchUserById('1'));
当我调用getState()
方法并解构users
函数中的状态片时,TSC抛出一个错误
属性“users”在类型“{}”上不存在。ts(2339)
推断的RootState
类型为:
type RootState = {
users: UsersSliceState;
}
我应该如何正确地制作此类型?如果我导入RootState
类型并对getState()
的返回值进行类型转换,是否会导致循环引用
软件包版本:
“@reduxjs/toolkit”:“^1.5.0”,
“类型脚本”:“^4.1.2”
您需要在to beRootState
中指定状态
,其中状态
是定义thunkApi
字段类型的可选字段的一部分
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;
}
},
}
);
export const fetchUserById=createAsynchThunk<
用户,
一串
{state:RootState}
>(
“用户/fetchByIdStatus”,
异步(用户ID,thunkAPI)=>{
const response=await userAPI.fetchById(userId);
返回响应数据;
},
{
条件:(userId,{getState})=>{
const{users}=getState();
const{fetchStatus}=users[userId];
如果(fetchStatus==='loading'){
返回false;
}
},
}
);
relevant docs link:@phry感谢提醒,刚刚将其添加到答案中。我将users.slice.ts
导入store.ts
文件中。如果我从users.slice.ts
文件中的'store.ts'导入RootState
。它会导致循环引用吗?@slideshowp2我真的不确定,必须检查一下,但这是从documentations@slideshowp2在这样做的时候,我从来没有遇到过这个问题。