Home > Mobile >  Parameter 'changes' implicitly has an 'any' type.ts(7006)
Parameter 'changes' implicitly has an 'any' type.ts(7006)

Time:01-28

Getting this error please tell me how to resolve this thank you.

  getClients(): Observable<Client[]> {
    // get clients with id
    this.clients = this.clientsCollection.snapshotChanges().map(changes => {
      return changes.map(action => {
        const data = action.payload.doc.data() as Client;
        data.id = action.payload.doc.id;
        return data;
      });
    });

    return this.clients;
  }

enter image description here

CodePudding user response:

Seems you run your angular project on strict you have couple of options.

Better practice is to get use to strict mode that can save you a lot of headache down the road, although some times it will cause the headache it self but trust me its worth it in the long run.

  1. In your case add :any before:
getClients(): Observable<Client[]> {
    // get clients with id
    this.clients = this.clientsCollection.snapshotChanges().map(changes:any => {
      return changes.map(action:any => {
        const data = action.payload.doc.data() as Client;
        data.id = action.payload.doc.id;
        return data;
      });
    });

    return this.clients;
  }
  1. In your tsconfig.json add this "noImplicitAny": false to "compilerOptions":{} this will cancel the alerts from the compiler.

  2. Create interface of the right type and work with it. this way the compiler can correct you if needed and prevent you making mistakes or trying to access a null ref.

best practice is option 3 but not always its right or worth building the interface just take it into consideration. And try to avoid :any when ever you can.

CodePudding user response:

Try changing the code to:

snapshotChanges().map((changes:any) => {...})

and

returns changes.map((action: any) ...
  •  Tags:  
  • Related