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



我有一个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
}

有什么技巧可以解决这个问题吗?


当我在构造函数中使用可选?时,例如:

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']
);
}

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

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

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']
);
}

我找到了解决方案:

发生了什么

当您创建一个具有可选参数的构造函数并尝试调用此构造函数时,它将按照调用的顺序设置属性。为此,当我打电话给时

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

在firstName或密码中设置的角色。

解决方案

为了解决这个问题,需要更改构造函数中的顺序或参数:

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']
);

直到您到达属性的位置。

使用:

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

然后你就这样使用:

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

最新更新