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

Can someone please tell me that

[index : string] : IFoo means in

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

I looked through the Typescript book, opened it and found nothing in this notation. Is this a collection of objects that implement IFoo? Thanks.

+6
source share
1 answer

Used to display the result type when indexing an instance of an interface. When elements of type IBar are indexed with the string ie [someString] , the result will be of type IFoo. eg:

 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'] 

More details: http://blogs.msdn.com/b/typescript/archive/2013/01/24/interfaces-walkthrough.aspx Go to the heading "Description of the indexed object"

+8
source

Source: https://habr.com/ru/post/945112/


All Articles