I want to update the student state correctly, a student is an object that contains user, city and grades objects. I can do it in the updateStudent function, but the parameter data is not strict and has a type any.
I want to restrict the parameter data to IUser | ICity | IGrade, what would be the best procedure for this case?
interface IUser {
name?: string;
id?: number;
}
interface ICity {
city?: string;
zip?: number;
}
interface IGrade {
math?: string;
biology?: string;
}
interface IStudent {
user: IUser;
city: ICity;
grades: IGrade;
}
const [student, setStudent] = useState<IStudent>({
user: {},
city: {},
grade: {},
});
const updateStudent = (data: any, key: string) => {
setStudent({ ...student, [key]: data });
};