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/