typescript - 如何在打字稿上传递无序参数?
问题描述
我有一个构造函数。我发送一些参数。如何在不更改其位置的情况下传递类型变量?
构造函数等待宽度变量,但我想发送类型变量
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;
}
解决方案
如果通过undefined
,则可以跳过默认初始化的参数并获取其默认值。
new ScpColumn(name, field, undefined, type);
推荐阅读
- csv - QGIS:csv中的“添加分隔文本文件”忽略csvt文件中指定的精度
- rust - 如何在 Rust 中计算 21 阶乘?
- javascript - 检查列表中键最多的对象的最佳方法是什么?
- c++ - 互斥锁上的默认析构函数被阻止
- r - 如何通过变量分隔现有数据框来使用循环创建多个数据框
- ionic4 - 在 ionic 4 中缓存图像
- android - ARCore 支持的设备 华为相关
- python - 为什么 Sympy 不能积分 1/\sqrt{x^2+x}
- javascript - 需要帮助使用随机从数组中获取随机字符串
- java - 错误:无法解决:com.google.firebase-core:16.0.2 受影响的模块:app