1

How to create new instance in TypeScript. Obviously I'm not matching the constructor but I can't see what's wrong.

export class User implements IUser {
    public id: number;
    public username: string;
    public firstname: string;
    public lastname: string;
    public birthday: string;
    public email: string;

    public constructor(iUser: IUser)
    {
        this.id = iUser.id;
        this.username = iUser.username;
        this.firstname = iUser.firstname;
        this.lastname = iUser.lastname;
        this.birthday = iUser.birthday;
        this.email = iUser.email;
    }
}

    interface IUser {
        id?: number;
        username: string;
        firstname: string;
        lastname: string;
        birthday: string;
        email: string;
    }

And student class that extends user

export class Student extends User implements IStudent, IUser {
    public indeks: string;
    public studyProgram: StudyProgram;


    public constructor(iUser: IUser, iStudent: IStudent)
    {
        super(iUser);
        this.indeks = iStudent.indeks;
        this.studyProgram = iStudent.studyProgram;
    }
}

    interface IStudent {
        indeks: string;
        studyProgram: StudyProgram;
    }

So, when I try to create new instance of student I got this error

 Supplied parameters do not match any signature of call target.

     this.student = new Student ({
      username: '',
      firstname: '',
      lastname: '',
      birthday: '',
      email: '',
      indeks: '',
      studyProgram: new StudyProgram({
        name: '',
        duration: 0,
        courseType: ''
    })
  });

And here is StudyProgram class

export class StudyProgram implements StudyProgramInterface {
        public id: number;
        public name: string;
        public duration: number;
        public courseType: string;
        public studentList: Array<Student>;

    public constructor(studyProgramCfg: StudyProgramInterface) {
        this.id = studyProgramCfg.id;
        this.name = studyProgramCfg.name;
        this.duration = studyProgramCfg.duration;
        this.courseType = studyProgramCfg.courseType;
    }
}

    interface StudyProgramInterface {
        id?: number;
        name: string;
        duration: number;
        courseType: string;
    }

1 Answer 1

1

The Student class constructor is expecting two objects (one implementing IStudent, and the other implementing IUser), you are passing only one. I think the constructor you are looking for is:

public constructor(student: IUser & IStudent) {
    super(student);
    this.indeks = student.indeks;
    this.studyProgram = student.studyProgram;
}

You can find more about intersection types here: https://basarat.gitbooks.io/typescript/content/docs/types/type-system.html

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.