ApplicationInsights-JS icon indicating copy to clipboard operation
ApplicationInsights-JS copied to clipboard

[BUG] error TS2748: Cannot access ambient const enums when the '--isolatedModules' flag is provided.

Open drmcclelland opened this issue 2 years ago • 8 comments

Encountered 6 errors when updating to v2.8.3 of @microsoft/applicationinsights-web (using tsc v4.6.4). These errors did NOT exist in v2.7.4, but do occur in all v2.8.x releases.


node_modules/@microsoft/applicationinsights-common/types/Enums.d.ts:8:101 - error TS2748: Cannot access ambient const enums when the '--isolatedModules' flag is provided.

8 export declare const StorageType: import("@microsoft/applicationinsights-core-js").EnumValue<typeof eStorageType>;
                                                                                                      ~~~~~~~~~~~~

node_modules/@microsoft/applicationinsights-common/types/Enums.d.ts:34:113 - error TS2748: Cannot access ambient const enums when the '--isolatedModules' flag is provided.

34 export declare const DistributedTracingModes: import("@microsoft/applicationinsights-core-js").EnumValue<typeof eDistributedTracingModes>;
                                                                                                                   ~~~~~~~~~~~~~~~~~~~~~~~~

node_modules/@microsoft/applicationinsights-common/types/Interfaces/Contracts/SeverityLevel.d.ts:14:103 - error TS2748: Cannot access ambient const enums when the '--isolatedModules' flag is provided.

14 export declare const SeverityLevel: import("@microsoft/applicationinsights-core-js").EnumValue<typeof eSeverityLevel>;
                                                                                                         ~~~~~~~~~~~~~~

node_modules/@microsoft/applicationinsights-core-js/types/JavaScriptSDK.Enums/EventsDiscardedReason.d.ts:33:111 - error TS2748: Cannot access ambient const enums when the '--isolatedModules' flag is provided.

33 export declare const EventsDiscardedReason: import("../JavaScriptSDK.Enums/EnumHelperFuncs").EnumValue<typeof eEventsDiscardedReason>;
                                                                                                                 ~~~~~~~~~~~~~~~~~~~~~~

node_modules/@microsoft/applicationinsights-core-js/types/JavaScriptSDK.Enums/LoggingEnums.d.ts:11:105 - error TS2748: Cannot access ambient const enums when the '--isolatedModules' flag is provided.

11 export declare const LoggingSeverity: import("../JavaScriptSDK.Enums/EnumHelperFuncs").EnumValue<typeof eLoggingSeverity>;
                                                                                                           ~~~~~~~~~~~~~~~~

node_modules/@microsoft/applicationinsights-core-js/types/JavaScriptSDK.Enums/LoggingEnums.d.ts:100:108 - error TS2748: Cannot access ambient const enums when the '--isolatedModules' flag is provided.

100 export declare const _InternalMessageId: import("../JavaScriptSDK.Enums/EnumHelperFuncs").EnumValue<typeof _eInternalMessageId>;
                                                                                                               ~~~~~~~~~~~~~~~~~~~

drmcclelland avatar May 12 '22 20:05 drmcclelland

Reading through multiple comments online there are 3 possible solutions to this for your application / library as this was a deliberate change for Application Insights to better support minification and tree-shaking.

  • Disable / don't use isolatedModules flag (I assume you don't want to do this)
  • Add skipLibCheck to true to your tsconfig.json file
  • Found a reference that "importing" the const enums into your project also causes TypeScript to be "happy"... Seem to have closed the reference so can't include it here.

MSNev avatar May 17 '22 19:05 MSNev

I'm also investigating to see if there is something I can do like declaring the exports as export type {...}, if that works I'll add to a future release. I need to validate with multiple versions of TypeScript so I don't break our older users.

MSNev avatar May 17 '22 20:05 MSNev

Using export type does not work and defining the packages with isolatedModules set to true causes the const enums to be included as full normal enums. Removing the refactoring and reason for changing to const enums (so they are replaced at compile time with the numeric values for minification).

So apart from the original suggestions, I don't have any other work-arounds at this point.

MSNev avatar May 17 '22 23:05 MSNev

Removing the refactoring and reason for changing to const enums (so they are replaced at compile time with the numeric values for minification).

@MSNev - does this mean this will be fixed in an upcoming build?

drmcclelland avatar May 27 '22 20:05 drmcclelland

No, at this point in time this is a won't fix as we will not be removing the usage of const enums.

And because we consume and extend this ourselves internally, one or more of the work arounds above should work. Internally we don't use isolatedModules and we have different usages of skipLibCheck, and based on readings it appears that if you directly import the const enums (which do appear to be exported when I looked) this should also work.

Please feel free to create a minimal repro that shows the issue and I can get someone to research into this a bit deeper for possible solutions / work arounds. I do know that we could stop using the EnumHelpers and manually create the objects, but this is problematic from a validation perspective as it would allow new values to be missed, where the current helpers where designed to break compiling when there was either a missed value or a typo for one of them.

I have refrained from tagging this as won't fix, as I expected that at some point a future version of TypeScript may provide an option to more directly address this issue as we are not the only library that has hit this issue.

MSNev avatar May 27 '22 22:05 MSNev

@MSNev I have the same issue even when I don't import const enums directly This is the import I use import { ApplicationInsights } from '@microsoft/applicationinsights-web';

saranchonkau avatar Jun 08 '22 10:06 saranchonkau

@saranchonkau Can you post your tsconfig.json (or at the relevant settings (module, target, isolatedModules, skipLibCheck, etc) as I would not expect that just importing the ApplicationInsights module (should) cause the reference to be included.

And @Karlie-777 if you can please create a repro with the settings so that we can investigate further.

MSNev avatar Jun 08 '22 17:06 MSNev

@MSNev @saranchonkau The error will occur when run tsc --isolatedModules. The solution is to remove the --isolatedModules flag or add skipLibCheck to true in tsconfig file as Nev suggested above. We change string variables to enum for minification purpose, so I don't think we want to change back to string variables.😊

Karlie-777 avatar Jun 08 '22 21:06 Karlie-777

This Issue will be closed in 30 days. Please remove the "Stale" label or comment to avoid closure with no action.

github-actions[bot] avatar Apr 05 '23 07:04 github-actions[bot]

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

github-actions[bot] avatar May 06 '24 00:05 github-actions[bot]