简体   繁体   English

在打字稿Angular 2中创建对象的实例

[英]Create instance of object in typescript Angular 2

How to create new instance in TypeScript.如何在 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这是StudyProgram课程

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;
    }

The Student class constructor is expecting two objects (one implementing IStudent, and the other implementing IUser), you are passing only one. Student 类构造函数需要两个对象(一个实现 IStudent,另一个实现 IUser),您只传递一个。 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您可以在此处找到有关交叉点类型的更多信息: https : //basarat.gitbooks.io/typescript/content/docs/types/type-system.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM