I have following two TypeScript classes:
export class AbsenceWithUser {
@jsonProperty(Absence) public readonly absence: Absence;
@jsonProperty(User) public readonly user: User;
constructor(absence: Absence, user: User) {
this.absence = absence;
this.user = user;
}
}
export class Birthday {
@jsonProperty(MomentDateJson) public readonly birthday: moment.Moment;
@jsonProperty(User) public readonly user: User;
constructor(birthday: moment.Moment, user: User) {
this.birthday = birthday;
this.user = user;
}
}
AbsenceWithUser
Birthday
private getAllEvents():(AbsenceWithUser|Birthday)[] {
const absences = this.props.absences as (AbsenceWithUser|Birthday)[];
const birthdays = this.props.birthdays as (AbsenceWithUser|Birthday)[];
return absences.concat(birthdays);
}
getAllEvents
private eventToDate(x:AbsenceWithUser | Birthday):Moment {
if (x instanceof AbsenceWithUser) {
return x.absence.endDate;
} else if (x instanceof Birthday) {
return x.birthday;
} else {
throw new Error("Unknown event type, x: " + x);
}
}
const events = this.getAllEvents();
this.eventToDate(events[0]);
AbsenceWithUser
Birthday
eventToDate
instanceof
getEvents
private getAllEvents():(AbsenceWithUser|Birthday)[] {
const absences:(AbsenceWithUser|Birthday) = this.props.absences as (AbsenceWithUser|Birthday)[];
const birthdays:(AbsenceWithUser|Birthday) = this.props.birthdays as (AbsenceWithUser|Birthday)[];
return absences.concat(birthdays);
}
Error:(127, 15) TS2322:Type '(AbsenceWithUser | Birthday)[]' is not assignable to type 'AbsenceWithUser | Birthday'.
Type '(AbsenceWithUser | Birthday)[]' is not assignable to type 'Birthday'.
Property 'birthday' is missing in type '(AbsenceWithUser | Birthday)[]'.
concat
const a:(AbsenceWithUser|Birthday) = [];
Error:(127, 15) TS2322:Type 'never[]' is not assignable to type 'AbsenceWithUser | Birthday'. Type 'never[]' is not assignable to type 'Birthday'. Property 'birthday' is missing in type 'never[]'.
never[]
getAllEvents
private getAllEvents():(AbsenceWithUser|Birthday)[] {
const absences:(AbsenceWithUser|Birthday)[] = this.props.absences as (AbsenceWithUser|Birthday)[];
const birthdays:(AbsenceWithUser|Birthday)[] = this.props.birthdays as (AbsenceWithUser|Birthday)[];
const allEvents:(AbsenceWithUser|Birthday)[] = absences.concat(birthdays);
return allEvents;
}
export type EventTypes = (AbsenceWithUser|Birthday);
private isEmpty(arr:EventTypes[]):boolean {
return arr.length === 0;
}
private getAllEvents():EventTypes[] {
const absences:EventTypes[] = this.props.absences as EventTypes[];
const birthdays:EventTypes[] = this.props.birthdays as EventTypes[];
const absencesEmpty:boolean = this.isEmpty(absences);
const birthdaysEmpty:boolean = this.isEmpty(birthdays)
if (absencesEmpty && !birthdaysEmpty) {
return birthdays;
} else if (absencesEmpty && birthdaysEmpty) {
return [];
} else if (!absencesEmpty && !birthdaysEmpty) {
return absences.concat(birthdays);
} else if (!absencesEmpty && birthdaysEmpty) {
return absences;
}
return [];
}
private getAllEvents():EventTypes[] {
const absences:EventTypes[] = this.props.absences.map(absenceJson => {
return new AbsenceWithUser(absenceJson.absence, absenceJson.user);
});
const birthdays:EventTypes[] = this.props.birthdays.map(birthdayJson => {
return new Birthday(birthdayJson.birthday, birthdayJson.user);
});
const absencesEmpty = this.isEmpty(absences);
const birthdaysEmpty = this.isEmpty(birthdays);
if (absencesEmpty && birthdaysEmpty) {
return [];
} else if (absencesEmpty && !birthdaysEmpty) {
return birthdays;
} else if (!absencesEmpty && birthdaysEmpty) {
return absences;
} else {
return absences.concat(birthdays);
}
}
Object.assign
I have created an example that shows how to merge two arrays of a specific type into an array that can have items of both types. You can use instanceof
to check the type.
class AbsenceWithUser {
public readonly when: string;
public readonly user: string;
}
class Birthday {
public readonly day: string;
public readonly user: string;
}
class Test {
constructor(){
let absences : AbsenceWithUser[] = [new AbsenceWithUser(), new AbsenceWithUser()]
let birthdays : Birthday[] = [new Birthday(), new Birthday(), new Birthday()]
let combined:(AbsenceWithUser | Birthday)[] = this.mergeArrays(absences, birthdays)
if (combined[0] instanceof Birthday) {
console.log("we have a birthday")
} else {
console.log("we have an absence")
}
}
private mergeArrays(absences: AbsenceWithUser[], birthdays: Birthday[]): (AbsenceWithUser | Birthday)[] {
var allEvents : (AbsenceWithUser | Birthday)[] = [...birthdays].concat(...absences);
return allEvents;
}
}