Не могу понять причину из-за которой возникает ошибка в следующей строке кода:
state[action.payload.index][action.payload.fieldToChangeName] = action.payload.value
Получаю ошибку:
Type 'string' is not assignable to type 'never'.
.
Код самого слайса:
export const contactsSlice = createSlice({
name: "contacts",
initialState: initialState,
reducers: {
add: (state, action: PayloadAction<IContact>) => {
state.push(action.payload)
},
remove: (state, action: PayloadAction<string>) => {
return state.filter((el: IContact) => el.id !== action.payload)
},
change: (state, action: PayloadAction<IChangeAction>) => {
state[action.payload.index][action.payload.fieldToChangeName] = action.payload.value
}
}
})
Типизация контакта и экшна:
interface IChangeAction {
index: number
fieldToChangeName: keyof IContact
value: string
}
export interface IContact {
firstName: string
lastName?: string
phoneNumber: string
description?: string
age?: number
tags?: string[]
id: string
}
Изначальный стор:
const initialState: IContact[] = [
{ firstName: 'Andrey', lastName: 'Feramonov', phoneNumber: '89771337004', description: 'Like bikes', age: 22, tags: ['helper'], id: '1' }
]
https://i.imgur.com/3spjT6c.png