-
Notifications
You must be signed in to change notification settings - Fork 490
const enums are not inlined #400
Comments
An interesting aspect, at least. Using However, most of this benefit goes to development mode. Production builds are faster as well, yet it might be acceptable to slow these down a bit in favor of correctness. Thus, it might be worth to disable that flag and the plugin for build mode. Just thinking about what to do with the linter execution in this case, which is also performed by the plugin (the linter execution is optional, but the type-checker cannot be disabled without disabling both). |
We also uses const enums for redux-actions so some bytes could be saved for other projects (no data on this though). But maybe this isn't that big of a problem for most projects, and maybe some information about the trade off, and if possible, an optional flag, with the effect of slowing down the build a bit. |
Note that Because of that, if these enums are not inlined properly (even in development mode!) the transpilation step will produce a type error, as that enum does not exist as a value anywhere in the code. It's definitely quite shocking that a baseline TypeScript feature just doesn't work here for no obvious reason. This creates a mismatch between tooling and compilation, which is pretty bad considering the whole reason we're using TypeScript is for type safety. Would definitely like to see |
See my description above about the reason. Still thinking about a good way to disable |
Yeah, I understand the technical reason, I just mean that there is no obvious reason as to why it doesn't work from a UX standpoint when debugging and running into this problem, especially because it results in a type error. |
I had a similar issue (an enum inside a declared but not exported namespace was undefined once transpiled), I asked on stackoverflow and the In my case, I exported the namespace (rather than declaring it) and exported the enum inside, as expected the generated code didn't have the inline value but at least the variable wasn't undefined anymore and the UI is working. As a temporary fix, I exported the namespace with all the declaration (enum included) and I have to manually import it in most of my modules. That import line in every file is a bit annoying, but at least it's not breaking... Don't know if it helps someone. |
Is this a bug report?
Yes.
const enums are not inlined when building (npm build / yarn build).
The reason can be understood here microsoft/TypeScript#11202
And the problem/feature is this line https://github.com/wmonk/create-react-app-typescript/blob/master/config/webpack.config.prod.js#L185
Our use case:
A large const enum that is generated and only a few values are actually used resulting in our bundle size to be a lot bigger than expected (+ 40kb). Plus we would like to think the values used to be just as strings while running and not accessed through an object.
Can you also reproduce the problem with npm 4.x?
Didn't seem relevant.
Which terms did you search for in User Guide?
const, enum, transpile
Environment
npm ls react-scripts-ts
(if you haven’t ejected):react-scripts-ts@2.17.0
node -v
:v8.11.1
npm -v
:5.6.0
yarn --version
(if you use Yarn):1.9.4
Then, specify:
macOS High Sierra
Steps to Reproduce
Import the enum in app and only use 'One'.
function(e){e.One="One",e.Two="Two"}(r||(r={}))}
And
t.prototype.render=function(){return i.a.One}
instead of the inlined version
Expected Behavior
If we manually changes the transpile flag to false and run yarn build again and inspects main.js
t.prototype.render=function(){return"One"}
And we got the inlined version :)
Actual Behavior
We uses the non inlined version (as described in step 5)
Reproducible Demo
Steps to Reproduce should be enough, otherwise I can push a demo project.
The text was updated successfully, but these errors were encountered: