[index: string]":打字稿中的IFoo符号

"[index : string]" : IFoo notation in typescript

本文关键字:IFoo 符号 string index quot      更新时间:2023-09-26

谁能告诉我

[index : string] : IFoo表示

export interface IBar {
   [index : string] : IFoo;
}
export interface IFoo {
        CharacterName: string;
        DisplayName: string;
    }

我翻遍了《Typescript Revealed》这本书,没有发现任何关于那个符号的内容。它应该是实现IFoo的对象集合吗?谢谢。

用于显示索引接口实例时结果的类型。当IBar类型的元素被字符串(即[someString])索引时,结果将是IFoo类型的。例句:

export interface IBar {
   [index : string] : IFoo;
}
export interface IFoo {
        CharacterName: string;
        DisplayName: string;
    }
    
    
var x:IBar; 
var y=x['asdf']; // Same as var y:IFoo = x['asdf']