I am trying to find an equivalent to Map<String, String>
in Dart, but usable in Typescript. This is a quick way to define a string:string key:value store.
Also, this can be used like this, which is my next question:
Map<String, List<String>>
This would define a key:value store where the keys are strings and the values are a list of strings.
Is there any way to do either in Typescript?
To define a Map object in TypeScript, use a generic to set the type of the Map's keys and values. All of the key-value pairs you add to the Map have to conform to the specified type, otherwise the type checker throws an error. Copied! We used a generic to declare a Map that has string keys and string or number values.
TypeScript map is a new data structure added in ES6 version of JavaScript. It allows us to store data in a key-value pair and remembers the original insertion order of the keys similar to other programming languages. In TypeScript map, we can use any value either as a key or as a value.
Creating a MapUse Map type and new keyword to create a map in TypeScript. let myMap = new Map<string, number>(); To create a Map with initial key-value pairs, pass the key-value pairs as an array to the Map constructor.
Map in Typescript Map is a new data structure introduced in ES 6 which lets you map keys to values without the drawbacks of using Objects.
If keys are just strings, you probably would prefer to use plain objects in TypeScript, which you can type as Record<string, string>
.
Example usage
The Typescript equivalent would be:
const m = new Map<string, string[]>();
m.set("fruit", ["apple", "banana"]);
Note that string[]
is the type "array of strings", which despite the name is a dynamically growable list of strings (unlike the fixed length arrays in many popular statically typed languages).
You can say Array<string>
instead but this is probably less widely used.
If your key is a string, you may find it more convenient to use a JS object. Objects in JS are basically maps with string keys. In TS the example becomes:
type StringToArrayOfStrings = {
[name: string]: string[]
};
const m: StringToArrayOfStrings = {};
m["fruit"] = ["apple", "banana"];
In the type
declaration we state the signature of the indexing operator [...]
. (It's possible to have numeric keys as well as string ones.)
UPDATE
Still using a plain object as a map, note that we don't have to use a type
declaration to give a name to a type. The type declaration can be substituted in-place, and we can then immediately initialise m
with a compatible object:
const m: { [name: string]: string[] } = {
"fruit": ["apple", "banana"],
"spaceship": ["orion", "apollo"]
};
Note that if we omit the type annotation on m
:
const m = {
"fruit": ["apple", "banana"],
"spaceship": ["orion", "apollo"]
};
then the type of m
is inferred to be more restrictive:
{
fruit: string[];
spaceship: string[];
}
the compiler will assume we mean that m
is permanently stuck with just two properties, fruit
and spaceship
, and will not allow us to add more. Interestingly, it will not assume anything about the length of the arrays (even it is possible to describe such things in TS via tuple types).
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