typescript - TypeScript界面​​中的方括号字段是什么

标签 typescript

export interface Principal {
  name: string; // just a field
  [attribute: string]: any; // allowed custom fields

  [securityId]: string; // what does this mean ?
}

securityId 是什么意思?

最佳答案

securityId 是计算的属性名称。

要编译此代码,securityId 必须是类型限制为文字字符串或唯一符号类型的值。

例如:

const securityId: "security" = "security";

interface Principal {
    [securityId]: string;
}

指定一个名为security的属性。

如果 securityId 没有文字类型,这将是一个错误:

const securityId: string = "security";

interface Principal {
    // ERROR: A computed property name in an interface must refer to an
    // expression whose type is a literal type or a 'unique symbol' type.
    [securityId]: string;
}

注意:我试图在规范中找到对此行为的引用,但它似乎已过时并且 not a priority - 欢迎编辑!

关于typescript - TypeScript界面​​中的方括号字段是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64180516/

相关文章:

javascript - RTCPeerConnection 与 RTCConfiguration 在 Firefox 上中断

javascript - typescript 泛型 : Use output argument type to build input argument type

javascript - 如何使用 Jquery 在 angular 5 上触发点击事件

angular - 之间的确切区别是什么?和 ? : operators in angular

javascript - 使用装饰器将类方法附加到窗口对象

javascript - 如何动态确保对象中的函数仅使用它接受的参数调用?

typescript - 如何安装所有基于 packages.json 的 typescript 定义文件?

typescript - 在 typescript 中使用泛型扩展接口(interface)

generics - TypeScript:当前类的类型作为类型变量

javascript - typescript - 泛型 - 对同一参数列表中的参数的引用