Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is the difference between { [k: string]: string } and Map<string, string>

I understand the normal Map<string, string> format. But i have seen some people use { [k: string]: string } for the same use case?

Are they same or different? i cannot find a proper documentation regarding it's use cases.

is it possible to convert one to another?

Does it have to do something with the object de-structuring ?

like image 350
Tilak Raj Avatar asked Sep 01 '25 20:09

Tilak Raj


1 Answers

In a basic sense, { [k: string]: string } is for an object, and Map<string, string> is for a Javascript Map primitive, which is not exactly the same.

For example, you could do Map<object, string>, because a Map can use an object as a key. However, { [k: object]: string } immediately throws an error, because javascript objects cannot use objects as their keys.

This does not have to do with destructuring, but rather with the difference between a standard javascript Object and a Map. I'm not sure what you mean when you say you've seen them used interchangeably, as this typescript playground throws an error when mixing them up.

like image 197
Seth Lutske Avatar answered Sep 04 '25 23:09

Seth Lutske



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!