-1

Can I create a type like a dictionary in typescript? For example the type of bar { [id: string] : IPerson; } which is a dictionary, how can I give it a name, let's say Dict and use that name instead of the whole { [id: string] : IPerson; }?

class Foo {
  bar:{ [id: string] : IPerson; }
}

The data I receive from the server look like this

{
  "15" : { name: "James", age: 45},
  "34" : { name: "Mary", age: 22},
  ...
}
koninos
  • 4,969
  • 5
  • 28
  • 47
  • Possible duplicate of [TypeScript Objects as Dictionary types as in C#](http://stackoverflow.com/questions/13631557/typescript-objects-as-dictionary-types-as-in-c-sharp) – Ivan Zlatev Jun 15 '16 at 10:27

1 Answers1

1

With an interface:

interface Dict { 
  [id: string] : IPerson
}
class Foo {
  bar: Dict
}
Paleo
  • 21,831
  • 4
  • 65
  • 76