Just imagine I have 2 html tables next to each other. Each table has an array of unassigned pupils or assigned pupils for a certain test. With 2 buttons you can move those checkbox-selected pupils to the other html table.
The pupils in both html tables have nearly the same properties.
When I use Object.Assign then I get an compile error:
properties gradeScorePairs is missing in type {} and UnAssignedPupil
To fix that error I just copy/paste the same properties on both objects...
Thats a workaround but not solution for me. I use Typescript 2 and transpile to ES5.
What possibilities do I have with object.assign and different types for source/target to solve this problem?
AssignedPupil.ts
import { GradeScorePair } from './gradeScorePair';
export class AssignedPupil {
constructor(obj: any) {
this.id = obj.id;
this.name = obj.firstName + " " + obj.lastName;
}
id: number;
name: string;
gradeScorePairs: GradeScorePair[];
isSelected: boolean;
selectedGradeScorePair: GradeScorePair;
}
UnAssignedPupil.ts
import { GradeScorePair } from './gradeScorePair';
export class UnAssignedPupil {
constructor(obj: any) {
this.id = obj.id;
this.name = obj.firstName + " " + obj.lastName;
}
id: number;
name: string;
// gradeScorePairs: GradeScorePair[]; // don`t need it on the unassigned html table
isSelected: boolean;
// selectedGradeScorePair: GradeScorePair; // don`t nee it on the unassigned html table
}
unassignPupil() {
var i = this.assignedPupils.length;
while (i--) {
var p = this.assignedPupils[i];
if (p.isSelected) {
let assignedPupil: AssignedPupil = this.assignedPupils.splice(i, 1)[0];
let unAssignedPupil: UnAssignedPupil = Object.assign({}, assignedPupil);
unAssignedPupil.isSelected = false;
this.unAssignedPupils.push(unAssignedPupil);
}
}
}
assignPupil() {
var i = this.unAssignedPupils.length;
while (i--) {
var p = this.unAssignedPupils[i];
if (p.isSelected) {
let pupilToAssign: UnAssignedPupil = this.unAssignedPupils.splice(i, 1)[0];
let assignedPupil: AssignedPupil = Object.assign({}, pupilToAssign);
// Difference are these 2 lines of code
assignedPupil.gradeScorePairs = this.gradeScorePairs;
assignedPupil.selectedGradeScorePair = null;
assignedPupil.isSelected = false;
this.assignedPupils.push(assignedPupil);
}
}
}