Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Can someone please tell me what the

[index : string] : IFoo means in

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

export interface IFoo {
        CharacterName: string;
        DisplayName: string;
    }

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.

like image 799
Crystal Avatar asked May 16 '13 01:05

Crystal


1 Answers

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']
like image 101
basarat Avatar answered Nov 15 '22 07:11

basarat