GitHub is home to over 40 million developers working together to host and review code, manage projects, and build software together. Have a question about this project?
Sign up for a free GitHub account to open an issue and contact its maintainers and the community. Already on GitHub? Sign in to your account.
In the docsit says that:. I don't believe that this is true. Babel should be able to transpile const enum s into objects, which will achieve the same functionality as typescript, through different transpiled output.
You can sign-up here for an invite. There is not way to transpile imported enum into something other than identifier or type. TrySound If const enum 's were transpiled into objects, they could be imported across modules, right? Then if you were using a bundler with scope hoisting, terser could still inline the values. This is similar to I believe? Also happy to take a PR for this. If we don't want to simply drop the const we could do an option. I want const enum s to be transpiled to plain objects, i.
I would be happy to look into submitting a PR I think that transpiling const enum s into plain objects should be the default behavior. What would be the output for this case? The whole point of const enum is to not have any code generated at all. Correct solution is to replace Direction. Down by 2 whenever it's used as a value. Consider the following code:. You need another file access to somewhere to know what Direction is, how it was defined, and what value to assign to Direction.
There is no way to do it otherwise. Babel compiles on a file basis, and doesn't read somewhere while it compiles other files. Thus support for const enum was dropped. While compiling somewhere it's still possible to generate an intermediate JSON file with a mapping like. There are still cyclic dependencies to be considered in such design as when A imports a value from B, while B imports type from Abut it should be already pretty obvious why proposed solution is wrong, and that it's actually quite tedious and ugly task.
When compiling a. The whole point of const enum is to not generate any code. They are a feature of type system.GitHub is home to over 40 million developers working together to host and review code, manage projects, and build software together.
This plugin does not support const enum s because those require type information to compile. Changes to your tsconfig. The build process will always behave as though isolatedModules is turned on, there are Babel-native alternative ways to set a lot of the tsconfig.
A : The TypeScript compiler dynamically changes how these variables are used depending on whether or not the value is mutated. Ultimately, this depends on a type-model and is outside the scope of Babel. A best-effort implementation would transform context-dependent usages of the variable to always use the Namespace.
Value version instead of Valuein case it was mutated outside of the current file. Allowing var or let from Babel as the transform is not-yet-written is therefore is more likely than not to present itself as a bug when used as-if it was not const.
If you have existing code which uses the TypeScript-only namespace features. Babel supports a subset of TypeScript's namespace features. It's not going awaybut there are modern alternatives. Type-only namespace s should be marked with declare and will subsequently be safely removed.
Not enabling will result in an error: "Namespace not marked type-only declare. Non-declarative namespaces are only supported experimentally in Babel. Change to const or Workaround : Use const. If some form of mutation is required, explicitly use an object with internal mutability.
In TypeScript, it is valid to refer to contextual items that a namespace extends without qualifying them, and the compiler will add the qualifier. In Babel, there is no type-model, and it is impossible to dynamically change references to match the established type of the parent object.
As Babel doesn't understand the type of Nthe reference to V will be undefined resulting in an error. Workaround : Explicitly refer to values not in the same namespace definition, even if they would be in the scope according to TypeScript. Forcibly enables jsx parsing. Also, isTSX: true requires allExtensions: true. Replace the function used when compiling JSX expressions.
This is so that we know that the import is not a type import, and should not be removed. When set to truethe transform will only remove type-only imports introduced in TypeScript 3.
You can read more about configuring plugin options here. When enabled, type-only class fields are only removed if they are prefixed with the declare modifier:.GitHub is home to over 40 million developers working together to host and review code, manage projects, and build software together. If nothing happens, download GitHub Desktop and try again. If nothing happens, download Xcode and try again. If nothing happens, download the GitHub extension for Visual Studio and try again. Removes the const keyword to use regular enum.
Can be used in a slower dev build to allow constwhile prod still uses tsc. See babel Transforms into a const object literal. You may be getting a SyntaxError because you are running this plugin on non-TypeScript source. You might have run into this problem in react-nativesee: babel-plugin-const-enum 2 babel-plugin-const-enum 3.
To fix this issue, please use babel-preset-const-enum to only run babel-plugin-const-enum on TypeScript files. If you wish to fix the issue manually, check out the solution in babel-plugin-const-enum 2. Skip to content. Dismiss Join GitHub today GitHub is home to over 40 million developers working together to host and review code, manage projects, and build software together.
Latest commit. Latest commit a Apr 12, Like many other compilers it runs in 3 stages: parsing, transforming, and printing. Now, out of the box Babel doesn't do anything. You will need to add plugins for Babel to do anything. Instead of individual plugins, you can also enable a set of plugins in a preset.
Transform plugins will enable the corresponding syntax plugin so you don't have to specify both. These plugins only allow Babel to parse specific types of syntax not transform. NOTE: transform plugins automatically enable the syntax plugins. So you don't need to specify the syntax plugin if the corresponding transform plugin is used already. Alternatively, you can also provide any plugins option from the Babel parser:. This means if two transforms both visit the "Program" node, the transforms will run in either plugin or preset order.
Will run in the following order: stage-2reactthen es This is mostly for ensuring backwards compatibility, since most users list "es" before "stage-0". For more information, see notes on potential traversal API changes. Both plugins and presets can have options specified by wrapping the name and an options object in an array inside your config.
Please refer to the excellent babel-handbook to learn how to create your own plugins. Guides What is Babel? Usage Options Config Files cli polyfill transform-runtime register. Presets env stage-0 stage-1 stage-2 stage-3 flow react minify typescript. Tooling parser core generator code-frame helpers runtime template traverse types. Transform Plugins These plugins apply transformations to your code. ES3 member-expression-literals property-literals reserved-words ES5 property-mutators ES arrow-functions block-scoped-functions block-scoping classes computed-properties destructuring duplicate-keys for-of function-name instanceof literals new-target object-super parameters shorthand-properties spread sticky-regex template-literals typeof-symbol unicode-regex ES exponentiation-operator ES async-to-generator ES async-generator-functions dotall-regex named-capturing-groups-regex object-rest-spread optional-catch-binding unicode-property-regex Modules modules-amd modules-commonjs modules-systemjs modules-umd Experimental class-properties decorators do-expressions export-default-from export-namespace-from function-bind function-sent logical-assignment-operators nullish-coalescing-operator numeric-separator optional-chaining partial-application pipeline-operator private-methods throw-expressions Minification Check out our minifier based on Babel!GitHub is home to over 40 million developers working together to host and review code, manage projects, and build software together.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community. Already on GitHub? Sign in to your account. I think this should have been fixed by Hey fiesh! We really appreciate you taking the time to report an issue. The collaborators on this project attempt to help as many people as possible, but we're a limited number of volunteers, so it's possible this won't be addressed swiftly.
For a better Dev UX, we should throw an const enums are not supported error. Here is where the error is thrown, we should make sure it will also throw for declare const enums. Lines 6 to 14 in a6e8b If you don't know how to clone Babel, follow these steps: you need to have make and yarn available on your machine.
Related: I used babel-plugin-const-enum but didn't see any difference, unless I used it incorrectly. Also removing the const from the enum doesn't change anything, so I don't understand what it meant by not supporting const enum.
I guess a const enum is different from a const enum in a way?! OK, the problem is that declare removes the generated code when const is removed. We generate a declaration file that contains them, and since it's a declaration file, of course we can only write declarations.
So I guess a workaround would be to generate an additional implementation file that contains the enum implementations as non- const. Hmm not ideal but doable. JLHwung I also think this should only be a warning, not an error, at least in the declare case. When one uses string enum s, one can also use the actual string value "e0" instead of E. Using strings as such should be legal and would work with babel I think. It seems like babel currently transforms declare const enum as if it was declare enum.
I compared the transforms on babel repl vs. TypeScript playground. Unlike the plain const enumdeclare const enum doesn't complain. Either way, babel doesn't support the inlining feature of const enums. The only way I know how to inline right now is by using babel-plugin-const-enum with a minifier. I think the correct way for babel is the following:. I am the creator of babel-plugin-const-enum. Currently, the remove-const transform works but it will give you the same result as what babel is currently doing, removing the const.
An enumeration implies a set of related constants, so the added information about the relationship must be useful in their model of the problem at hand. This meant that const was useless for constant expressions inside classes.
Thus, you will commonly see:. There's a historical reason too when dealing with template metaprogramming. Some compilers could use values from an enum, but not a static const int to instantiate a class. Another possible reason, is that a static const int may have memory reserved for it at runtime, whereas an enum is never going to have an actual memory location reserved for it, and will be dealt at compile time.
See this related question. Then it is easy to loop until LAST, and when a new state or whatever is represented is added, the logic adapts. If the constant is an integer type, a kludgy work around is define it in an enum inside the class:. So you can define a function that takes an enum as a parameter, which makes it more clear what kinds of values should be given as arguments to the function, as compared to having the values defined as const variables and the function accepting just "int" as an argument.
Some debuggers will show the enumeration name instead of its value when debugging. This can be very helpful. The other reason is that enums can be used as a convenient syntactic device to organise class constants into those that are related, and those that are not.
Using an enum documents the valid choices in a terse manner and allows the compiler to enforce them. If they are using enum store global constants, like Pi, for example, then I don't know what their goal is. Learn more. Ask Question. Asked 10 years, 10 months ago. Active 8 years, 7 months ago. Viewed 30k times. Trott Loai Nagati Loai Nagati 1, 4 4 gold badges 18 18 silver badges 23 23 bronze badges. I know this is a really old thread, but could you give an example?
When I read this question I was thinking along the lines of code that returns an int from a function, but uses an enum to define specific 'versions' of the return value, e. Some of the answers below seem to address totally different issues to that. In my opinion it's wrong to use an enum in that way unless, as people say, your compiler is limited in its functionality wrt static const.
Possible duplicate of Should I use define, enum or const? Active Oldest Votes. Hm, that does not look like an answer to the question though it was accepted. People use enums where they could use const and the values are not related. It's because enum never gets any storage while const variable is still a variable and will get static storage if the compiler can't proove it won't need one, which it often can't. Zifre This is not what the question asked. Eclipse Eclipse