0

I'm currently upgrading from webpack 3 to 5 using ruby on rails shakapacker gem which is the successor of webpacker (a wrapper around webpack)

I'm bumping into the issue that some of the enums I defined in my types.ts files are undefined when loading inside the browser.

Example:

Constants.ts

// Some source file
export const DEFAULT_LOCALE: Locale = Locale.EN;

types.ts

const enum Locale {
  EN = 'en',
  NL = 'nl',
  FR = 'fr',
  DE = 'de'
}

Error message

I see no typescript errors whatsoever. Loading the page results in this error being thrown.

It seems the enums are stripped for some reason, but I don't know what new configuration might have changed this.

This code works as expected on a previous version

Any help in the right direction would be greatly appreciated!

Kind regards Jens

anut
  • 481
  • 1
  • 11
Jens
  • 1,132
  • 7
  • 14
  • Remove the `const` from `const enum`. [This answer](https://stackoverflow.com/questions/40227401/const-enum-in-typescript) describes more about why. – Alex Wayne Jul 18 '22 at 20:29
  • @AlexWayne is this something that has been changed in newer versions of typescript? Because this worked previously. My code uses this feature extensively. I want to use Locale.EN as a value removing the const from `types.ts` does not fix the issue. – Jens Jul 19 '22 at 08:26
  • I can tell you that `const enum` does not emit the enum itself as far back as Typescript 3.3.3. See the JS output in https://tsplay.dev/mM4ndN. If you want to export the enum and reference it by name in other files, then you don't want them to be `const enum`. Remove that `const` and you should be fine. – Alex Wayne Jul 19 '22 at 17:29

0 Answers0