I'm trying update the values of a form, utilizing the Component as a class, and trying to do less code, tried to use Immer for this.
State Class
interface State {
term: Term,
...
}
Term Class
interface Term{
firstname: string,
surname: string,
address: Address,
...
}
Address Class
interface Address{
zipcode: string,
street: string,
...
}
The changes on Address are made by this handler
handleAddressChange = (newValue:string, key:keyof Address) => {
this.setState((prevVal) => {
prevVal.term.address = produce<Address>(prevVal.term.address,(draft) => {
draft[key] = newValue;
})
})
}
In the draft[key]...
line, i just receive the error 'Type 'string' cannot be assigned to type 'never' even typing the produce with .
How is possible to fix that?
UPDATE
this.setState(produce<State>(state =>{
const newState: State = { ...state };
newState.term.address = {...newState.term.address, [modelKey]: newValue}
console.log(newState)
return newState;
}))
Worked for now, if you know another way type below.