When I try to code, I see the following error: "Expected 3 arguments, but got 1" on the "refactorStudent" and "refactorInfo" mutode, tell me how to fix. And in general, do I use TypeScript correctly (just started).

//constructor for creating students class Student { constructor(public name: string, public surname: string, public age: number) { this.name = name; this.surname = surname; this.age = age; } //rewrite one or all options of student info public refactorStudent(name: string, surname: string, age: number): void { this.name = typeof name !== 'undefined' ? name : this.name; this.surname = typeof surname !== 'undefined' ? surname : this.surname; this.age = typeof age !== 'undefined' ? age : this.age; } //show info about student public toString(): string { return `\nName: ${this.name}, Surname: ${this.surname}, Age: ${this.age}`; } } //constructor for create groups with students class Group { public students: any[]; constructor(public nameGroup: string, public course: string, public specialization: string) { this.nameGroup = nameGroup; this.course = course; this.specialization = specialization; this.students = []; } //rewrite one or all options of group info public refactorInfo(nameGroup: string, course: string, specialization: string): void { this.nameGroup = typeof nameGroup !== 'undefined' ? nameGroup : this.nameGroup; this.course = typeof course !== 'undefined' ? course : this.course; this.specialization = typeof specialization !== 'undefined' ? specialization : this.specialization; } //add new student public addStudent(...students: any[]) { this.students.push(...students); } // show info about group with students public toString(): string { return `Name of Group: ${this.nameGroup}; \nCourse: ${this.course}; \nSpecialization: ${this.specialization};\nStudents: ${this.students}` } } // create students let student1 = new Student("Aladin", "Indus", 20); let student2 = new Student("Allah", "Babah", 25); let student3 = new Student("Krishna", "harehare", 23); // create group let Devs = new Group("D-11", "4", "Front-end"); //rename group name Devs.refactorInfo("D-12"); //push students into group Devs.addStudent(student1, student2, student3); //rename name of first student student1.refactorStudent("Super"); console.log(Devs.toString()); 

  • It seems you can add default values ​​of variables in the function, then in theory there will be no error - name: string = '', ... - Artem Gorlachev

1 answer 1

If you want the argument to be not mandatory then add "?" as I wrote below.

 public refactorStudent(name?: string, surname?: string, age?: number): void { // .... }