如何在typescript上传递无序参数?

如何在typescript上传递无序参数?,typescript,Typescript,我有一个构造函数。我发送了一些参数。如何传递类型变量而不更改其位置 构造函数等待宽度变量,但我想发送类型变量 constructor( name: string, field: string, width: number = 0, type: string = '', ) { this.name = name; this.field = field; this.width = width; this.type = type; } publi

我有一个构造函数。我发送了一些参数。如何传递类型变量而不更改其位置

构造函数等待宽度变量,但我想发送类型变量

constructor(
   name: string,
   field: string,
   width: number = 0,
   type: string = '',
   ) {
   this.name = name;
   this.field = field;
   this.width = width;
   this.type = type;
}

public static create(name: string, field: string, type?: string) {
    const scpCol = new ScpColumn(name, field, type); // constructor waits width variable but I want to send type variable
    return scpCol;
}

如果我理解正确,您应该这样定义构造函数:

  constructor(
   name: string,
   field: string,
   type: string = '',
   width?: number = 0,

   )

然后根据您的意愿调用它

如果我理解正确,您应该这样定义构造函数:

  constructor(
   name: string,
   field: string,
   type: string = '',
   width?: number = 0,

   )

并根据需要调用它

如果传递了
未定义的
,则可以跳过默认的初始化参数并获取其默认值

new ScpColumn(name, field, undefined, type);

如果传递了
未定义的
,则可以跳过默认的初始化参数并获取其默认值

new ScpColumn(name, field, undefined, type);

也许你能更具体地描述一下“改变位置”,甚至描述一下你的问题。thx@Atomzwieback嗨,我是编辑。我应该发送宽度变量'newscpcolumn(名称、字段、类型);'在程序上。但我想发送类型变量。请您更具体地说明“更改位置”或描述您的问题。thx@Atomzwieback嗨,我是编辑。我应该发送宽度变量'newscpcolumn(名称、字段、类型);'在程序上。但我想发送类型变量。