Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
drmcclelland opened this issue May 12, 2022 · 10 comments
Labels
closed Automatically Closed Issue/PR stale

Comments

@drmcclelland
Copy link

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>;
                                                                                                               ~~~~~~~~~~~~~~~~~~~
@MSNev
Copy link
Collaborator

MSNev commented May 17, 2022

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 MSNev self-assigned this May 17, 2022
@MSNev MSNev added the investigating Investigating the issue label May 17, 2022
@MSNev
Copy link
Collaborator

MSNev commented May 17, 2022

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
Copy link
Collaborator

MSNev commented May 17, 2022

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 MSNev removed the investigating Investigating the issue label May 18, 2022
@MSNev MSNev removed their assignment May 20, 2022
@drmcclelland
Copy link
Author

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?

@MSNev
Copy link
Collaborator

MSNev commented May 27, 2022

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.

@saranchonkau
Copy link

@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';

@MSNev
Copy link
Collaborator

MSNev commented Jun 8, 2022

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

@Karlie-777
Copy link
Contributor

@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.😊

@github-actions
Copy link

github-actions bot commented Apr 5, 2023

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

@github-actions github-actions bot added the stale label Apr 5, 2023
@github-actions github-actions bot added the closed Automatically Closed Issue/PR label May 6, 2023
@github-actions github-actions bot closed this as completed May 6, 2023
Copy link

github-actions bot commented May 6, 2024

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 github-actions bot locked as resolved and limited conversation to collaborators May 6, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
closed Automatically Closed Issue/PR stale
Projects
None yet
Development

No branches or pull requests

4 participants