Object as typed collection

Topics: Language Specification
Mar 3, 2014 at 6:33 PM
I can declare array as typed collection what is very handy
 var items:VoItem[]
but how can I declare that my object contains collection of specific type ?
var items:VoItem{} /// gives error 
Developer
Mar 3, 2014 at 7:24 PM
You do that using a string index signature:
var items: { [index: string]: VoItem };
Or, if you do this a lot, you could introduce a generic type and use that:
interface Dictionary<T> {
    [index: string]: T;
}
var items: Dictionary<VoItem>;
Mar 3, 2014 at 11:04 PM
Edited Mar 3, 2014 at 11:05 PM
Thank you for replay
I use it a lot by storing VoItem in collections by properties
for example

 VoItem {
prop1;
prop2,;
.........................
.................................
}


so I create collections for faster access: VoItem by it  unique property

var byProp1 :Dictionary<VoItem>
var byProp2 :Dictionary<VoItem>

getItemyByProp1(val:number):VoItem{
return this.byProp1[val];
}
getItemyByProp2(val:string):VoItem{
return this.byProp2[val];
}
in pure JAVASCRIPT I was using { } as collections and very surprised that Typescript has wonderful way of define typed array but they miss objects for some reason.