Hello everyone, I am sitting now for five days in front of working code, but typescript stopps me with an error. I want to prefill input fields with data from my db. I am using redux-toolkit for getting this data and it works. I have all the data in my input fields. But typescript tells me:
The property X does not exist for type User.ts(2339)
How I can tell typescript that my code works? What it want from me? Can someone help me out? Thanks
Here the code in react:
interface Props{
user:User,
onEdit: (user: User) => void,
setFormdata:React.Dispatch<React.SetStateAction<object>>
}
const UserDisplay:React.FC<Props> = () => {
const dispatch = useAppDispatch();
const selector = useAppSelector((state:RootState)=>state.user);
const {user, isError, isLoading, message} = selector;
const {id} = useParams();
useEffect(()=>{
if(isError){
toast.error(message);
}
dispatch(getUser(id!));
return ()=>{
dispatch(reset())
}
}, [dispatch, isError, message, id]);
const [formdata, setFormdata] = useState<{vorname:string, nachname:string, username:string, email:string, street:string, number:string,plz:string, city:string, isAdmin:boolean, createdAt:string}>({
vorname:"",
nachname:"",
username:"",
email:"",
street:"",
number:"",
plz:"",
city:"",
isAdmin:false,
createdAt:Date.toLocaleString(),
})
console.log(user);
const {vorname, nachname, username, email, street, number, plz, city, isAdmin, createdAt} = formdata;
useEffect(()=>{
if(user){
setFormdata({
vorname:user.vorname, //Here all values are underlined with the error above
nachname:user.nachname,
username:user.username,
email:user.email,
street:user.street,
number: user.number,
plz:user.plz,
city:user.city,
isAdmin:user.isAdmin,
createdAt:user.createdAt,
})
}
}, [user])
Here the interfaces and initialState in redux-toolkit, perhaps it’s there, but I don’t think so:
export interface User{
_id?:string,
id?:string,
vorname:string
nachname:string
username:string
email:string
street:string
number:string
plz:string
city:string
password:string
isAdmin:boolean
createdAt: Date
accessToken: string;
}
interface InitialState{
user:User[],
isLoading:boolean,
isSuccess:boolean,
isError:boolean,
message:string,
}
const initialState:InitialState ={
user:[],
isLoading:false,
isSuccess:false,
isError:false,
message:"",
}
Best Roman