Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to declare a typed object with arbitrary keys?

Tags:

typescript

In a .d.ts file, how do I define an object as having string keys and T values?

e.g.

declare var Game: {
    creeps: Object<string, Creep>; // not sure what syntax to use here
};

declare class Creep {
   // definition...
}

Game.creeps is an object, but I don't know what properties/keys it will have (they're defined at run time -- I'm using it like a dictionary), however, I do know that all its values will be Creeps.

My IDE says "Object is not generic" so I guess that syntax isn't quite right.

like image 892
mpen Avatar asked Apr 13 '16 06:04

mpen


1 Answers

Use an index signature:

declare var Game: {
    creeps: {[key:string]: Creep}
};
like image 82
basarat Avatar answered Sep 23 '22 09:09

basarat