The library cannot be imported using the definitely typed types when typescript has "strictNullChecks" set to true.
In this case Typescript shows a compilation error when accessing the imported AppInsights object with the message "Object is possibly undefined."
I actually just noticed that this is only when trying to call downloadAndSetup().
import { AppInsights } from 'applicationinsights-js';
AppInsights.flush();
AppInsights.trackMetric('test', 10, 1);
AppInsights.downloadAndSetup({instrumentationKey: 'instrumentationKey'}); // <- This has compilation error "Object is possibly undefined"
I had assumed that AppInsights could be undefined in the Typings but something else is wrong since the other methods aren't showing the same compilation error.
Maybe I am doing something wrong?
I just found a workaround here: https://github.com/DefinitelyTyped/DefinitelyTyped/issues/16056
if (AppInsights.downloadAndSetup !== undefined) {
AppInsights.downloadAndSetup({instrumentationKey: 'instrumentationKey'});
}
It looks like the typings are incorrect. In hindsight this probably wasn't the best place to raise this issue as I should have probably done it on the DefinitlyTyped repository. However, it would be great if you would consider bringing the typings inside this repository.
Most helpful comment
I just found a workaround here: https://github.com/DefinitelyTyped/DefinitelyTyped/issues/16056
if (AppInsights.downloadAndSetup !== undefined) {
AppInsights.downloadAndSetup({instrumentationKey: 'instrumentationKey'});
}
It looks like the typings are incorrect. In hindsight this probably wasn't the best place to raise this issue as I should have probably done it on the DefinitlyTyped repository. However, it would be great if you would consider bringing the typings inside this repository.