I have one common interface and two interfaces "Person" and "Employee" that extend the first one:
export interface Common {
id: number,
name: string
}
export interface Person extends Common {
age: number;
}
export interface Employee extends Common {
role: string;
}
I need to have an array mixed with these interfaces, for example this:
listOfPeople: Person[] | Employee[] = [{id: 1, name: 'George', age: 4}, {id: 2, name: 'Micheal', role: 'software developer'}];
but in this way I got an error. What's the correct way for obtaining the result that I desire?
PersonOrEmployeetype with optionalage?androle?parameters. But again, I'm VERY unsure.Array<Person|Employee>you cannotitem.ageoritem.rolebecausePersonandEmployeedo not have those keys in common. However, if you introducePersonOrEmployeeyou can checkitem.ageanditem.roleand do like thisif (item.age) { (item as Person) } else {.(item as Employee) }