【发布时间】:2021-03-09 15:17:01
【问题描述】:
我刚刚接近React-Redux,并且按照此处找到的示例:https://redux-toolkit.js.org/usage/usage-with-typescript 我正在尝试逐步构建我需要的东西。在一个全新的 infopieceSlice.ts 中,我添加了以下代码:
import { createSlice, PayloadAction } from '@reduxjs/toolkit';
import { AppThunk, RootState } from '../../store/store';
interface InfopieceState {
content: string[];
}
const initialInfopieceState: InfopieceState = {
content: [],
};
export const infopieceSlice = createSlice({
name: 'infopiece',
initialInfopieceState,
reducers: {
add: (InfopieceState, action: PayloadAction<string>) => {
InfopieceState.content.push(action.payload);
},
},
});
在 /store/store.ts 中的位置:
import { configureStore, ThunkAction, Action } from '@reduxjs/toolkit';
import counterReducer from '../features/counter/counterSlice';
import logger from 'redux-logger';
export const store = configureStore({
reducer: {
counter: counterReducer,
},
middleware: getDefaultMiddleware =>
getDefaultMiddleware()
.concat(logger)
});
export type RootState = ReturnType<typeof store.getState>;
export type AppThunk<ReturnType = void> = ThunkAction<
ReturnType,
RootState,
unknown,
Action<string>
>;
export type AppDispatch = typeof store.dispatch;
我得到这个错误:
src/features/counter/infopieceSlice.ts:19:3 - error TS2345: Argument of type '{ name:
string; initialInfopieceState: InfopieceState; reducers: { add: (InfopieceState: unknown,
action: { payload: string; type: string; }) => void; }; }' is not assignable to parameter of
type 'CreateSliceOptions<unknown, SliceCaseReducers<unknown>, string>'.
Object literal may only specify known properties, and 'initialInfopieceState' does not
exist in type 'CreateSliceOptions<unknown, SliceCaseReducers<unknown>, string>'.
19 initialInfopieceState,
~~~~~~~~~~~~~~~~~~~~~
src/features/counter/infopieceSlice.ts:22:7 - error TS2571: Object is of type 'unknown'.
22 InfopieceState.content.push(action.payload);
这是执行所采用的相应代码,如下所示:https://redux-toolkit.js.org/introduction/getting-started,
npx create-react-app my-app --template redux-typescript:
import { createSlice, PayloadAction } from '@reduxjs/toolkit';
import { AppThunk, RootState } from '../../store/store';
interface CounterState {
value: number;
}
const initialState: CounterState = {
value: 0,
};
export const counterSlice = createSlice({
name: 'counter',
// `createSlice` will infer the state type from the
// `initialCounterState` argument
initialState,
reducers: {
increment: state => {
// Redux Toolkit allows us to write "mutating" logic in
reducers.
// It doesn't actually mutate the state because it uses the
Immer library,
// which detects changes to a "draft state" and produces a brand
new
// immutable state based off those changes
state.value += 1;
},
decrement: state => {
state.value -= 1;
},
// Use the PayloadAction type to declare the contents of
//`action.payload`
incrementByAmount: (state, action: PayloadAction<number>) => {
state.value += action.payload;
},
},
});
我的目标是有一个 InfopieceState 和 infopieceSlice 来处理 string[] ,字符串数组。
如何修复这些类型?
【问题讨论】:
-
initialState: initialInfopieceState,而不是initialInfopieceStateincreateSlice -
@RobertoZvjerković 谢谢。这是一个愚蠢的错误。所以
initialState是键值对的键...如果你把它作为答案,我会把它标记为正确的。再次感谢您的帮助 -
是的,
initialState是关键,如果你只写initialInfopieceState那是initialInfopieceState: initialInfopieceState的简写,而initialInfopieceState因为关键不存在。 -
@RobertoZvjerković 非常感谢!!!
-
@RobertoZvjerković 请将此添加为答案。谢谢。