TypeScript:不允许多个构造函数实现

owfi6suc  于 2023-02-05  发布在  TypeScript
关注(0)|答案(3)|浏览(557)

我有一个Object,我在多个服务中使用它,每个服务都需要一些参数,所以我创建了两个构造函数,但TypeScript不允许我这样做。

class User {
    id: number;
    username: string;
    password: string;
    email: string;
    firstName: string;
    lastName: string;
    roles: string[];

    constructor(username: string, password: string){
        this.username = username;
        this.password = password;
    }

    constructor(id: number, username: string, firstname: string, lastname: string, roles: string[]){
        this.id = id;
        this.username= username;
        this.firstname= firstname;
        this.lastname= lastname;
        this.roles = roles;
    }
    //.. and maybe another constructor also
}

请问有什么窍门可以解决这个问题吗?
例如,当我在构造函数中使用 optional?时:

constructor(
    public id?: number,
    public username?: string,
    public email?: string,
    public password?: string,
    public firstName?: string,
    public lastName?: string,
    public roles?: string[]) {
}

当我从后端获得数据时:

this.service.usersList().subscribe(users => {
  console.log(users);
  this.dataSource.data = users;
});

roles设置在密码中,而不是失败的角色中:

{
  "id": 1,
  "username": "user1",
  "email": "user1@email.com",
  "password": [
    "USER",
    "MODR"
  ]
}

所以我对这个把戏不太确定。
也许我不够精确,但是我使用这个方法来解析我的数据:

static fromJson(item: Object): any {
    return new User(
        item['id'],
        item['username'],
        item['email'],
        item['roles']
    );
}

为此,当我创建一个具有optional的构造函数时,它将按照我调用的顺序设置属性。

jdgnovmf

jdgnovmf1#

不能使用多个构造函数,但可以添加一些可选参数并验证它是否存在,如下所示:

class User {
    id: number;
    username: string;
    password: string;
    email: string;
    firstname: string;
    lastname: string;
    roles: string[];
    // The "?" says that its optional parameter
    constructor(id?: number, username?: string, firstname?: string,
        lastname?: string, roles?: string[], password?: string) {
        if (id) { // if id exists , you can implement the first constructor
            this.id = id;
            this.username = username;
            this.firstname = firstname;
            this.lastname = lastname;
            this.roles = roles;
        }
        if (password) { // if password exists : you can implement the second one
            this.username = username;
            this.password = password;
        }
    }
}

您的响应应该如下所示,然后才能正常工作:

static fromJson(item: Object): any {
    return new User({
        id : item['id'],
        username : item['username'],
        email : item['email'],
        roles : item['roles']
    });
}

所以你的构造函数应该是这样的:

constructor(user: any){
    if (user.id) { // if id exists , you can implement the first constructor
        this.id = user.id;
        this.username = user.username;
        this.firstname = user.firstname;
        this.lastname = user.lastname;
        this.roles = user.roles;
    }
    if (user.password) { // if password exists : you can implement the second one
        this.username = user.username;
        this.password = user.password;
    }
}

或者,如果您不想这样做,您可以设置有关订单的响应,如下所示:

static fromJson(item: Object): any {
    return new User(
        item['id'],
        item['username'],
        undefined,
        item['email'],
        undefined,
        undefined,
        item['roles']
    );
}
uwopmtnx

uwopmtnx2#

我找到了解决办法:

发生了什么?

当你创建一个带可选参数的构造函数并试图调用这个构造函数时,它会按照调用的顺序设置属性。

new User(
    item['id'],
    item['username'],
    item['email'],
    item['roles']
);

在firstName或password中设置的角色。
溶液
要解决这个问题,需要更改构造函数中的顺序或参数:

constructor(
    public id?: number,
    public username?: string,
    public email?: string,
    public roles?: string[],

    public password?: string,
    public firstName?: string,
    public lastName?: string) {
}

或者,如果您不想更改顺序,就使用undefined作为示例:

new User(
    item['id'],
    item['username'],
    undefined,
    item['email'],
    undefined,
    undefined,
    item['roles']
);

直到到达属性的位置。

llmtgqce

llmtgqce3#

use:

export class A {
   constructor() {
      // Something here
   }
   secondConstructor() {
      // Something here
      return this;
   }
}

然后你就像这样使用:

const a = new A();
const a2 = new A().secondConstructor();

相关问题