I have to type : School and Campus. But one School can have many campuses and one Campus can only have one school.
In my code, I need to manipulate either a Campus with its School data embedded in it, or a School with an embedded array with its Campuses data in it. This is how I implemented my types and interfaces.
type Campus = {
zip_code: number;
address: string;
name: string;
[key: string]: string | number;
};
type School = {
name: string;
interests: number;
contactEmail: string;
[key: string]: string | number;
};
interface CampusSchool extends Campus {
school: School;
}
interface SchoolCampuses extends School {
campuses: Campus[];
}
This gives me the error "The "school" property of type "School" cannot be assigned to the index type "string", "string | number".ts(2411)".
I tried another way to create y interface, based on this answer : TS 2411 - getting errors property 'propertyName' of type 'string' is not assignable to string index type :
interface CampusSchool extends Campus {
school: { [schoolData: string]: {
data: School;
}
}
}
However, this doens't work and I still have my error. Also, I feel like it is not the proper way to do it so I ask for your help and advices. Thank you in advance for your help.