Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

create and initialize dictionary in typescript in same line

I want to create a dictionary using TypeScript and initialize it in same line instead of first creating and then populating the value like below

var persons: { [id: string] : IPerson; } = {};
persons["p1"] = { firstName: "F1", lastname: "L1" };

How do I combine the above into one?

like image 925
user1892775 Avatar asked Aug 21 '17 18:08

user1892775


1 Answers

Just create an object. Objects in ECMAScripts are associative arrays.

Here is your example as object:

const persons: { [id: string] : IPerson; } = {
  p1: { firstName: "F1", lastname: "L1" }
};

It's also better to use const or let instead of var.

like image 133
derkoe Avatar answered Nov 15 '22 04:11

derkoe