I'm coming from mobile app development and do not have much experience with typescript. How one can declare a map object of the form [string:any] ?
The ERROR comes at line: map[key] = value;
Element implicitly has an 'any' type because expression of type 'string' can't be used to index type 'Object'.
No index signature with a parameter of type 'string' was found on type 'Object'.ts(7053)
var docRef = db.collection("accidentDetails").doc(documentId);
docRef.get().then(function(doc: any) {
if (doc.exists) {
console.log("Document data:", doc.data());
var map = new Object();
for (let [key, value] of Object.entries(doc.data())) {
map[key] = value;
// console.log(`${key}: ${value}`);
}
} else {
// doc.data() will be undefined in this case
console.log("No such document!");
} }).catch(function(error: any) {
console.log("Error getting document:", error);
});
The error "No index signature with a parameter of type 'string' was found on type" occurs when we use a value of type string to index an object with specific keys. To solve the error, type the string as one of the object's keys using keyof typeof obj .
The "Type 'string' is not assignable to type" TypeScript error occurs when we try to assign a value of type string to something that expects a different type, e.g. a more specific string literal type or an enum. To solve the error use a const or a type assertion.
The error "Argument of type is not assignable to parameter of type 'never'" occurs when we declare an empty array without explicitly typing it and attempt to add elements to it. To solve the error, explicitly type the empty array, e.g. const arr: string[] = []; .
You generally don't want to use new Object()
. Instead, define map
like so:
var map: { [key: string]: any } = {}; // A map of string -> anything you like
If you can, it's better to replace any
with something more specific, but this should work to start with.
You need to declare a Record Type
var map: Record<string, any> = {};
https://www.typescriptlang.org/docs/handbook/utility-types.html#recordkeystype
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