Использую redux-toolkit, вот моя функция сортировки:
sortedPosts(state,action:PayloadAction<string>) {
state.filterPosts = state.posts.sort((a,b) => a[action.payload].localeCompare(b[action.payload]))
}
ts ругается, подчеркивая a[action.payload] и b[action.payload]: "Element implicitly has an 'any' type because expression of type 'string' can't be used to index type 'WritableDraft'. No index signature with a parameter of type 'string' was found on type 'WritableDraft'"
Что можно сделать, что-бы исправить проблему?
interface PostsState {
posts: IPosts[];
filterPosts: IPosts[];
isLoading: boolean;
error: string;
}
const initialState: PostsState = {
posts:[],
filterPosts:[],
isLoading: false,
error: ''
}
export const postSlice = createSlice({
name: 'posts',
initialState,
reducers: {
addPost(state, action:PayloadAction<IPosts>) {
state.filterPosts.unshift(action.payload)
},
removePost(state, action:PayloadAction<number>) {
state.filterPosts = state.filterPosts.filter(post => post.id !== action.payload)
},
searchPosts(state, action:PayloadAction<string>) {
state.filterPosts = state.posts.filter(post => post.title.toLowerCase().includes(action.payload))
},
sortedPosts(state,action:PayloadAction<string>) {
state.filterPosts = state.posts.sort((a,b) => a[action.payload].localeCompare(b[action.payload]))
}
},
extraReducers: {
[fetchPosts.fulfilled.type]:(state, action:PayloadAction<IPosts[]>) => {
state.isLoading = false;
state.error = '';
state.posts = action.payload;
state.filterPosts = action.payload;
},
[fetchPosts.pending.type]:(state) => {
state.isLoading = true;
},
[fetchPosts.rejected.type]:(state,action:PayloadAction<string>) => {
state.isLoading = false;
state.error = action.payload;
}
}
})
export interface IPosts {
id: number,
title: string,
body: string
}
id