Romano
Romano

Reputation: 47

Typescript error : get two type depending on each others

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.

Upvotes: 0

Views: 77

Answers (2)

Romano
Romano

Reputation: 47

I think that this isn't the right implementation of what I am trying to achieve. I want to use the School sometimes a School only, campuses aside, and sometime as a School with Campuses. So instead of crossing my types weirdly, I am just going to create to different type for different uses.

type Campus = {
  zip_code: number;
  address: string;
  name: string;
};

type School = {
  name: string;
  interests: number;
  contactEmail: string;
  school: CampusSchool;
};

type CampusSchool = Campus & {
  school: School;
};

type SchoolCampuses = School & {
  campuses: Campus[];
}

Upvotes: 0

Mert
Mert

Reputation: 121

In your implementation, the issue is that you are trying to assign the "school" property of type "School" to the index type "string | number". To resolve this issue, you can modify the "School" type and add the "school" property to it with a specific type:

type Campus = {
  zip_code: number;
  address: string;
  name: string;
};

type School = {
  name: string;
  interests: number;
  contactEmail: string;
  school: CampusSchool;
};

interface CampusSchool extends Campus {
  school: School;
}

interface SchoolCampuses extends School {
  campuses: Campus[] | [];
}

This way, you are not using the index type and the error should be resolved.

Also, for the SchoolCampuses interface, you can define the campuses property with a type of Campus[]. The | [] is not necessary because Campus[] is already an array type, so it will be an empty array by default if no campuses are specified.

Upvotes: 1

Related Questions