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?
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With