对象与数组的约束 array/obj[索引]
,具有一个索引签名,规定了索引(对象属性/数组索引)与返回值的类型
interface MyInterface {
[ name: number|string ]: type // 索引签名
}
- name 随意,第一个type是 索引/属性名 的类型,第二个是对应返回值的类型
- 索引只支持 number、string 两种类型;可以同时存在,但是number必须是string的子类型
示例
interface MyInterface {
[ index: number ]: string
}
const obj: MyInterface = {
0: 'xxx',
1: 'yyy'
}
const arr: MyInterface = ['xxx', 'yyy']