Tilak Raj
Tilak Raj

Reputation: 1499

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 ?

Upvotes: 4

Views: 3289

Answers (1)

Seth Lutske
Seth Lutske

Reputation: 10714

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.

Upvotes: 4

Related Questions