【发布时间】:2019-09-17 07:23:03
【问题描述】:
是否可以根据一些枚举来限制接口字段名称(不将枚举的每个值都指向名称)?例如:
enum childSumNames {
child_incoming_costs = "isv_child_incoming_costs",
child_est_revenue = "isv_child_est_revenue",
child_margin = "isv_child_margin",
}
/** Only field names from childSumNames enum are allowed */
interface IChildSumFieldMapper {
[key in childSumNames]: string // something like this (but it is incorrect syntax)...
/* instead of this:
* [childSumNames.child_incoming_costs]: string,
* [childSumNames.child_est_revenue]: string,
* [childSumNames.child_margin]: string, */
}
// The example of IChildSumFieldMapper instance
const item: IChildSumFieldMapper = {
[childSumNames.child_incoming_costs]: "isv_incomingcost",
[childSumNames.child_est_revenue]: "isv_estimatedrevenue",
[childSumNames.child_margin]: "isv_margin",
}
【问题讨论】:
-
@HarunYilmaz 谢谢。我更新了我的代码,但它仍然无效。我也添加了屏幕。
标签: typescript