本文介绍了“[index:string]" :打字稿中的IFoo表示法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有人可以告诉我这是什么
Can someone please tell me what the
[index:string]:IFoo
表示
export interface IBar {
[index : string] : IFoo;
}
export interface IFoo {
CharacterName: string;
DisplayName: string;
}
我查看了Typescript Revealed书,但没有发现任何符号。它应该是实现IFoo的对象集合吗?谢谢。
I looked through the Typescript Revealed book and found nothing on that notation. Is it supposed to be a collection of objects that implement IFoo? Thanks.
推荐答案
它用于显示索引接口实例时的结果类型。当IBar类型的元素由字符串索引,即 [someString]
时,结果将为IFoo类型。
例如:
It is used to show the type of the result when an instance of the interface is indexed. When elements of type IBar are indexed by a string i.e [someString]
the result will be of type IFoo. e.g:
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']
更多:转到标题描述可转换对象
这篇关于“[index:string]" :打字稿中的IFoo表示法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!