0
votes

I have an array of objects

student = [{
rollNo: Number,
name: String,
dateOfJoining: Number
}];

Now I'm trying to sort my array inside my function using this code.

students(){
for( let item of res.data){
this.event.push({
        name: item.name,
        rollNo: item.rollNO,
        date: item.joining,
      });
}
this.student.sort((val1, val2) => {return val2.dateOfJoining - val1.dateOfJoining});
}

But I'm getting the error "The left-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type." I know it's because my dateOfJoining is 'Number' and not 'number'.

But I can't write it as 'number' because then I get the error "'number' only refers to a type, but is being used as a value here."

1

1 Answers

3
votes

Number is a constructor for the JavaScript number type. This is not the value that you intend.

interface Student {
  rollNo: number;
  name: string;
  dateOfJoining: number;
}

student: Student[] = [{
  ...
}];

Note that this will require a default value for student, but an empty array will also work, e.g.

student = Student[] = [];
student.push({ rollNo: 1, name: 'Andrew', dateOfJoining: 2 });

At this point you will have to fill in student with the actual values which should be numbers and strings.