typescript - How to understand syntax in TypeScript interface [key: string]
问题描述
I'm having trouble deciphering a TypeScript syntax I found within an interface declaration here.
interface FormattingOptions {
tabSize: number;
insertSpaces: boolean;
[key: string]: boolean | number | string;
}
Can someone explain me the third parameter of this interface? The one containing [key: string] ...
? How is this type of syntax called?
解决方案
这是一个索引签名。这意味着除了接口的已知属性之外,任何其他类型的属性boolean
,number
或string
可以存在
interface FormattingOptions {
tabSize: number;
insertSpaces: boolean;
[key: string]: boolean | number | string;
}
let f: FormattingOptions = {
tabSize: 1,
insertSpaces: true,
other: '' // witout the index signature this would be invalid.
};
推荐阅读
- node.js - Socrata nodejs - 将浮动时间戳转换为日期时间格式
- javascript - Puppeteer - 使用“--allow-file-access-from-files”通过 XMLHttpRequest 加载本地文件不起作用
- scip - SCIP 6.0 无法读取的 SMPS 文件
- alias - 带有选项的别名的 kubectl bash 补全
- javascript - pug 迭代 - 循环遍历具有动态长度的对象属性
- user-interface - 在 wxpython 面板上不正确地刷新 GUI 图
- windows - ipython启动很慢,有没有办法加速呢?
- python - Python tensorflow 复数运算
- php - 如何将 $arr['news'] 更改为 echo
- c# - WPF 应用程序无限进度条不显示,但进程正在后台运行