使用部分<;T>;在TypeScript中进行灵活的初始化
我试图在构造函数中使用使用部分<;T>;在TypeScript中进行灵活的初始化,typescript,Typescript,我试图在构造函数中使用init参数的Partial类型,使初始化更加灵活。这是我的密码: export class Team { id: string; name: string; public constructor(init?: Partial<Team>) { } } const team = new Team({}); 导出类团队{ id:字符串; 名称:字符串; 公共构造函数(初始化?:部分){ } } const team=新团队(
init
参数的Partial
类型,使初始化更加灵活。这是我的密码:
export class Team {
id: string;
name: string;
public constructor(init?: Partial<Team>) {
}
}
const team = new Team({});
导出类团队{
id:字符串;
名称:字符串;
公共构造函数(初始化?:部分){
}
}
const team=新团队({});
但我不确定为什么会出现此错误:
Argument of type {} is not assignable to parameter type Partial<Team>
{}类型的参数不能分配给参数类型Partial
代码对我来说很好。试过在命令行中编译,但我的IDE仍然给我那个错误,所以它是一个和IDE问题。