【发布时间】:2018-02-03 18:49:58
【问题描述】:
如何处理 Typescript 中的可选参数?
我有以下型号:
export class PowerPlant {
id: number;
orgName: string;
minPower: number;
maxPower: number;
powerPlantType: string;
rampRateInSeconds: number;
rampPowerInKw: number;
}
在此模型中,最后两个字段,即 rampRateInSeconds 和 rampPowerInKw 将是可选值,并且取决于 powerPlantType 字符串。我希望能够在构造函数中实例化它,在构造函数中,我将检查 powerPlantType 字符串,如果它是某种类型,我想设置这两个附加字段。
来自函数式编程背景,我会将这两种类型作为可选类型,并在 powerPlantType 字符串上进行模式匹配,并相应地设置这两种值。我怎样才能在 Typescript 中达到同样的效果?
【问题讨论】:
-
I want to be able to instantiate this in the constructor and in the constructor, -
你到底坚持什么?例如,了解如何定义可选参数很容易。
-
为什么会被否决?
标签: typescript optional