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

Fix enums for consumers using isolated modules. #5

Merged
merged 1 commit into from
Sep 18, 2021

Conversation

marionebl
Copy link
Contributor

When importing @lastolivegames/becsy in a composite project or a project using the isolatedModules flag, the following error is encountered:

node_modules/@lastolivegames/becsy/index.umd.d.ts:104:37 - error TS2748: Cannot access ambient const enums when the '--isolatedModules' flag is provided.

104 type QueryFlavorName = keyof typeof QueryFlavor;
                                        ~~~~~~~~~~~

This PR moves all const enum to enum to avoid the above interoperability issue.

Let me know what you think!

@pkaminski
Copy link
Collaborator

I definitely want to support isolated modules mode, which is why I have "isolatedModules": true in tsconfig.json. I don't understand why that doesn't error on the export const enum. Conversely, I don't understand why you get an error for module-local const enum declarations, as these are indeed isolated to the module.

While I'd like to understand why the TS compiler isn't working as expected that can often be a foolish quest. I'm OK with this simple change as long as it doesn't affect performance -- did you try yarn perf to see? If it does, I'd like to find another approach.

@pkaminski
Copy link
Collaborator

This TS issue looks potentially relevant, but it's not (yet) resolved.

@marionebl
Copy link
Contributor Author

did you try yarn perf to see? If it does, I'd like to find another approach.

Sure thing, details below. If I interpret this correctly my machine is ~20% slower at executing JavaScript than what you used to create the baseline. 😉 Can't see a regression going from 3df15d4 to 364fb1.

`3df15d4` → `364fb1` benchmarks

Before 3df15d4

                        dev                   perf
ecs-benchmark
  packed1             4,775 op/s ( -18%)    73,403 op/s ( -30%)
  packed5             4,969 op/s (  -9%)    69,870 op/s ( -31%)
  simpleIter          1,227 op/s ( +15%)    51,221 op/s ( -12%)
  fragIter            6,235 op/s ( -44%)   147,718 op/s ( -23%)
  entityCycle           379 op/s ( -30%)     1,936 op/s
  addRemove           2,738 op/s (+103%)     8,072 op/s ( -13%)

ecs-packed
  packed1             4,965 op/s ( -11%)    55,412 op/s ( -18%)
  packed5             4,819 op/s ( -10%)    51,138 op/s ( -22%)
  simpleIter            959 op/s (  -5%)    27,797 op/s ( -29%)
  fragIter            9,019 op/s ( -12%)    83,361 op/s ( -35%)
  entityCycle           363 op/s ( -31%)     1,869 op/s
  addRemove           2,591 op/s (+103%)     7,443 op/s ( -14%)

ecs-packed-elastic
  packed1             4,484 op/s ( -19%)    34,239 op/s ( -23%)
  packed5             4,434 op/s ( -18%)    35,093 op/s ( -20%)
  simpleIter          1,159 op/s ( +21%)    19,884 op/s ( -10%)
  fragIter            9,307 op/s (  -9%)    65,050 op/s ( -23%)
  entityCycle           375 op/s ( -27%)     1,698 op/s
  addRemove           2,684 op/s (+113%)     7,521 op/s ( -12%)

After 364fb1

                       dev                   perf
ecs-benchmark
  packed1             5,179 op/s ( -17%)    77,992 op/s ( -31%)
  packed5             5,140 op/s ( -13%)    71,627 op/s ( -34%)
  simpleIter          1,354 op/s ( +18%)    55,067 op/s ( -12%)
  fragIter           10,028 op/s ( -16%)   149,103 op/s ( -28%)
  entityCycle           394 op/s ( -33%)     2,010 op/s
  addRemove           2,620 op/s ( +80%)     8,173 op/s ( -18%)

ecs-packed
  packed1             4,906 op/s ( -18%)    56,530 op/s ( -22%)
  packed5             4,893 op/s ( -15%)    52,032 op/s ( -27%)
  simpleIter          1,207 op/s ( +11%)    28,590 op/s ( -33%)
  fragIter            9,700 op/s ( -12%)   125,642 op/s (  -8%)
  entityCycle           376 op/s ( -33%)     1,921 op/s
  addRemove           2,703 op/s ( +97%)     7,949 op/s ( -14%)

ecs-packed-elastic
  packed1             4,766 op/s ( -20%)    33,801 op/s ( -30%)
  packed5             4,743 op/s ( -19%)    34,845 op/s ( -26%)
  simpleIter            900 op/s ( -12%)    13,317 op/s ( -44%)
  fragIter            6,932 op/s ( -37%)    61,322 op/s ( -32%)
  entityCycle           383 op/s ( -31%)     1,788 op/s (  -7%)
  addRemove           2,663 op/s ( +96%)     7,280 op/s ( -21%)

I don't understand why that doesn't error on the export const enum. Conversely, I don't understand why you get an error for module-local const enum declarations, as these are indeed isolated to the module.

This is the best explanation of the issue https://ncjamieson.com/dont-export-const-enums/#isolated-modules I've read to date. I suspect we end up implicitly exporting QueryFlavor via QueryBox['results'] → QueryFlavorName → QueryFlavor, thus the type is in fact accessed which triggers the error reported.

@pkaminski
Copy link
Collaborator

Thanks! I agree that performance seems to hold up, though it looks like my efforts to adjust benchmark results according to a dynamic empty loop baseline are completely ineffective. I'll merge and rerun the benchmarks on my machine to double check.

And yeah, I found that blog post too, but I still can't make sense of what we're seeing. I do have isolatedModules set yet tsc isn't warning me about the issues. I could work around the QueryFlavor export by manually constructing the type instead, but I don't want to waste time fighting the language so let's just go with non-const enums throughout as the most straightforward solution.

@pkaminski pkaminski merged commit 5552329 into LastOliveGames:main Sep 18, 2021
@pkaminski
Copy link
Collaborator

Released in 0.8.3 -- thanks again!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants