type DocumentData = { [field: string]: any };
let data1: DocumentData = {4:3};
console.log(data1); //{4:3}
key is string type, value is any type, Doesn't it mean that the DocumentData type in the above code is the object type of the above type? I know Dart, so isn't it the Map<String, dynamic> type in Dart?
However, when I ran the above code on the site below, it seems that no error occurred. Am I misunderstanding something? https://playcode.io/1081552
As indicated above.As explained above, I can't grasp what kind of data structure the DocumentData type indicates.
CodePudding user response:
The DocumentData type in the above code is an object type, which is a collection of key-value pairs. The keys are of type string and the values can be any type. This data structure allows you to store any kind of information associated with each key, making it very flexible and powerful. It's similar to the Map<String, dynamic> type in Dart; however, while both types allow for storing multiple pieces of information as name/value pairs (or "key-values"), only DocumentData permits different types for its values – including objects or other complex structures such as arrays – whereas Map<String, dynamic> requires all stored items must have a value that conforms to the Dynamic Type System (i.e., anything).
CodePudding user response:
Read this: What do square brackets mean where a field should be in typescript?
TL:DR DocumentData
is an object with an indexable field.
So when you instantiate data1
with 4
as the key, 4
is a valid index.