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

Can't use with isolated modules #38

Open
broady opened this issue Aug 31, 2021 · 13 comments
Open

Can't use with isolated modules #38

broady opened this issue Aug 31, 2021 · 13 comments

Comments

@broady
Copy link

broady commented Aug 31, 2021

I'm new to typescript, so I don't fully understand this compilation error:

Type error: Cannot access ambient const enums when the '--isolatedModules' flag is provided.

  83 |             const playerResp = await searchDestinyPlayer($http, {
  84 |                 displayName: encodeURIComponent(name),
> 85 |                 membershipType: BungieMembershipType.All,
     |                                 ^
  86 |             })
  87 |             if (!playerResp.Response.length) {
  88 |                 console.log("Couldn't find player (retry?)")

but it led me here:
https://ncjamieson.com/dont-export-const-enums/

@bhollis
Copy link
Contributor

bhollis commented Aug 31, 2021

Yes, we decided to export const enums because it has a very large impact on the code size for DIM.

You'll need to use https://www.npmjs.com/package/babel-plugin-const-enum - try looking at DIM's config to see how we're consuming this.

@raky291
Copy link

raky291 commented Aug 12, 2022

Hi everyone, I created my project from scratch using create-react-app and I have the same issue too.

I'm checking the create-react-app repository to see how other devs handle the issue.

I'm still searching a workaround. 👀

FYI: I have this property activated and I still have the issue "skipLibCheck": true,

facebook/create-react-app#5738

@jbccollins
Copy link

This is a pretty hard blocker when working with this library in Next.js :(
It is impossible to set "isolatedModules": false, when using Next. This is an older comment but it's still relevant today. Next will go in and forcibly change your tsconfig if you set isolatedModules to false.

@bhollis
Copy link
Contributor

bhollis commented Oct 27, 2022

You do not need to set isolatedModules: false but you do need to enable the const enum optimization for babel-typescript. See the DIM source for examples on usage.

@bhollis
Copy link
Contributor

bhollis commented Oct 27, 2022

Closing since the library is usable from Babel-only transpilation. See https://github.com/DestinyItemManager/DIM/blob/master/babel.config.js

@bhollis bhollis closed this as completed Oct 27, 2022
@jbccollins
Copy link

Ok. I assume this is the relevant part of the babel config for DIM?

plugins.push(['@babel/plugin-transform-typescript', { isTSX: true, optimizeConstEnums: true }]);

I tried following the nextjs instructions on how to customize the babel config.

Here's the .babelrc I came up with:

{
    "presets": ["next/babel"],
    "plugins": [
        [
            "@babel/plugin-transform-typescript",
            { "optimizeConstEnums": true }
        ]
    ]
}

But I still get the same error about isolated modules. Any thoughts on why that might be the case?

@jbccollins
Copy link

Ok for now I've "solved" this for me at least.

I ended up just forking the repo and replacing the usage of export const enum with export enum. It does indeed balloon the size of the project but at the moment I'm not too concerned about that for my use case.

For posterity: If anyone has this issue in the future you can try to use my fork bungie-api-ts-no-const-enum

@bhollis bhollis reopened this Oct 29, 2022
@bhollis
Copy link
Contributor

bhollis commented Oct 29, 2022

I built up a test next.js project, and I see the problem you're talking about. I got confused - you're right that enabling isolatedModules in tsconfig.json causes errors to be printed. Why I was confused is that it actually works just fine - the right values are used, and everything compiles and runs just fine. tsc just complains. If you could turn off isolatedModules but still use Babel - as DIM does - you'll get the right results and silence the warnings. But next.js forces isolatedModules back on and doesn't give you the option to ignore it.

I'll think through a workflow for publishing a no-const-enum version of this library, though it's very sad that you won't be able to use the more optimized version because of something that's not preventing any functional issue.

BTW, I think this is how you configure the TypeScript babel config in next.js:

"presets": [
    [
      "next/babel",
      {
        "typescript": { "optimizeConstEnums": true }
      }
    ]
  ],

@jbccollins
Copy link

You're the best!

@jbccollins
Copy link

Any updates on this one?

@bhollis
Copy link
Contributor

bhollis commented May 12, 2023

No, still thinking through how best to retain the massive code size optimization of inlining the numeric values without publishing a whole different version of the library. I'll be sure to update this when I have a solution.

@M7ilan
Copy link

M7ilan commented Nov 22, 2023

Having the same problem. Any solutions found?

@bhollis
Copy link
Contributor

bhollis commented Nov 22, 2023

I'll be sure to update this when I have a solution.

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

No branches or pull requests

5 participants