You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

20 lines
537 B

2 years ago
  1. // 索引签名类型
  2. // 使用场景:无法确定对象中有哪些属性(或者对象中出现任意多个属性) 例:用户输入的内容
  3. interface AnyObject {
  4. // 字符串的键都可以出现在 AnyObject 对象中
  5. [key: string] : number // key 表示一个占位符(可以更改名字),number 表示属性的值的类型
  6. }
  7. let obj: AnyObject = {
  8. a: 1,
  9. abc: 123,
  10. }
  11. // 在数组中使用
  12. interface MyArray<Type> {
  13. [index: number]: Type
  14. }
  15. let arr1 : MyArray<number> = [1, 3, 5]
  16. arr1[0]