Pass different objects into same prop - TS error
Is it allowed to pass objects of different structure into the same prop, conditionally?
Something like this
if (case = "teacher") {
return (
<div>
<TeacherComponent person={teacher}/>
</div>
);
}
else if (case = "student") {
return (
<div>
<StudentComponent person={student}/>
</div>
);
}
Note that teacher and student have different properties and do not have the same structure. Typescript is getting mad at me when doing this 🙂1 Reply
Nevermind - i solved ut using the union operator!
`interface Teacher {
name: string;
age: number;
subject: string;
}
interface Student {
name: string;
age: number;
grade: number;
}
type Person = Teacher | Student;
interface Props {
person: Person;
}
'