首页 > 解决方案 > 如何在打字稿上传递无序参数?

问题描述

我有一个构造函数。我发送一些参数。如何在不更改其位置的情况下传递类型变量?

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

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

标签: typescript

解决方案


如果通过undefined,则可以跳过默认初始化的参数并获取其默认值。

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

推荐阅读