Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

No index signature with a parameter of type 'string' was found on type

Tags:

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);
  });
like image 626
Andrei Enache Avatar asked Sep 03 '19 10:09

Andrei Enache


People also ask

Which type string has no matching index signature?

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 .

Is not assignable to type string?

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.

Is not assignable to type never []'?

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[] = []; .


2 Answers

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.

like image 156
Tim Perry Avatar answered Sep 20 '22 04:09

Tim Perry


You need to declare a Record Type

var map: Record<string, any> = {};

https://www.typescriptlang.org/docs/handbook/utility-types.html#recordkeystype

like image 33
aWebDeveloper Avatar answered Sep 21 '22 04:09

aWebDeveloper