我在找到索引并尝试减小数字值时遇到此错误。我正在使用带有响应的打字稿。
类型'number'不是数组类型。 TS2461
这是代码
减速器
import produce from "immer";
import * as types from "../actionTypes/postActionTypes";
export interface postState {
posts: Array<any>;
}
const initialState: postState = {
posts: []
};
const postReducer = (state = initialState, action: any): postState =>
produce(state, draft => {
switch (action.type) {
.....
case types.DISLIKE_POST_SUCCESS:
console.log(action);
const newfindKey = state.posts.findIndex(
x => x.id === action.payload.id
);
console.log(newfindKey);
draft.posts[newfindKey] = [...(draft.posts[newfindKey].likeCounts - 1)];
return;
});
export default postReducer;
答案 0 :(得分:0)
代替散布对象,我只是这样做了。现在,它会减少likeCounts值。
case types.DISLIKE_POST_SUCCESS:
console.log(action);
const newfindKey = state.posts.findIndex(
x => x.id === action.payload.id
);
draft.posts[newfindKey].likeCounts = draft.posts[newfindKey].likeCounts - 1;