繁体   English   中英

为什么我不能在与其他键的接口中使用枚举作为键

[英]Why can't I use an enum as key in a interface with other keys

我通过其他问题发现了如何使其工作,但我找不到原因。

我有一个enum我想用作接口中的对象键:

export enum actions {
  insert = 'insert',
  merge = 'merge',
  assign = 'assign',
}

我的这个配置界面工作:

interface Config {
  calls: {
    reads: storeName[]
    writes: storeName[]
  } & {
    [action in actions]?: storeName[]
  }
}

但这不起作用:

interface Config {
  calls: {
    reads: storeName[]
    writes: storeName[]
    [action in actions]?: storeName[]
  }
}

它给出了错误:

类型文字中的计算属性名称必须引用类型为文字类型或“唯一符号”类型的表达式。ts(1170) 计算属性名称必须是“字符串”、“数字”、“符号”类型,或“任何”。

任何人都可以帮助我理解为什么会发生这种情况。

基本上:

  • { reads: storeName[], writes: storeName[] }是一个普通的接口类型
  • { [action in actions]?: storeName[] }是一个映射类型

你不能写出两者兼而有之的东西; 虽然你可以取每一个的交集。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM