0

I use app insights library in my angular project. And I use eslint to lint the code which really helps find some weird bugs:

@Injectable({ providedIn: "root" })
export class AppInsightsService {
   appInsights: ApplicationInsights;
   constructor() {
      this.appInsights = new ApplicationInsights({   // ====> HERE
         config: {
            instrumentationKey: environment.appInsights.instrumentationKey,
            enableAutoRouteTracking: true
         }
      });
      this.appInsights.loadAppInsights();            // ====> HERE
   }

   logPageView(name?: string, url?: string): void {
      this.appInsights.trackPageView({               // ====> AND HERE
         name,
         uri: url
      });
   }
...
}

But lint shows the following errors for the lines where I added comments:

Unsafe assignment of an any value.eslint@typescript-eslint/no-unsafe-assignment)

Can someone explain please what's wrong with the appInsights field?

AlexB
  • 4,167
  • 4
  • 45
  • 117
  • Most likely, it is complaining about a param type of `any` being passed into `new ApplicationInsights()`. It wants the param to be a defined type/interface. – Samuel Goldenbaum Sep 22 '21 at 19:47
  • `ApplicationInsights` has this definition for `constructor(snippet: Snippet)`. Not sure I understand what should I change... Also, same error for `this.appInsights.loadAppInsights();` – AlexB Sep 22 '21 at 19:56
  • Can you try something like: `const config: Snippet = {...` or `const config = new Snippet(...` and pass that to `new ApplicationInsights(config)` – Samuel Goldenbaum Sep 22 '21 at 20:05
  • I tried but there's no such thing as `Snippet`... :/ I mean I do see it in the definition but TypeScript can't find it. – AlexB Sep 22 '21 at 20:32

0 Answers0