Applicationinsights-js: Object is possibly undefined when importing using Typescript and "strict null checks"

Created on 12 Feb 2018  路  2Comments  路  Source: microsoft/ApplicationInsights-JS

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."

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.

All 2 comments

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.

Was this page helpful?
0 / 5 - 0 ratings