【发布时间】:2022-01-10 13:01:25
【问题描述】:
我有这种类型:
type Period = 'Monthly' | 'Yearly'
type Cycle = {
period: Period,
price: number
}
我想得到以下类型(基本上,允许句点为空字符串):
type Period = 'Monthly' | 'Yearly'
type EditableCycle = {
period: Period | '',
price: number
}
我正在考虑类似这样的组合语法:
type EditableCycle = Extend<Cycle, { period: '' }>
我怎样才能实现它?
原因,我希望用户在编辑时不能选择任何句点(因此句点可以是空字符串),但在验证后我确定句点将保持有效句点。
注意:真实类型比 Cycle 复杂得多,涉及数组和类似的东西,这是一个简化的例子。
【问题讨论】:
标签: typescript types