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

Dont remove export interface , instead rename it to something else. #830

Closed
renhiyama opened this issue Feb 23, 2024 · 4 comments
Closed

Comments

@renhiyama
Copy link

Due to how Sucrase can't read other related files, it cant remove import statements that imports the interfaces.
So sucrase seems to only remove the exported variable completely, while not removing the imports, hence breaking the code.
The simplest and easiest suggestion I can give is to rename the export interface <var> {...} to something like to export const <var> = 0; So it doesn't break anything while removing typescript code as expected.
Screenshot_20240223_211239_Termux

@easrng
Copy link

easrng commented Apr 26, 2024

Can't you just import types explicitly as types import { type GlobOptions } from "..."?

@renhiyama
Copy link
Author

Can't you just import types explicitly as types import { type GlobOptions } from "..."?

that's not what this issue is about. Sucrase seems to break any code that exports interfaces when converting any typescript code to javascript. In order to not break any compability issues, my suggestion posted above is the only solution.

Your suggestion doesn't make sense because sucrase's point is to make stuff work, without needing much changes, nor break anything up.

@renhiyama
Copy link
Author

@alangpierce could you please look at this issue asap?

@alangpierce
Copy link
Owner

Hi @renhiyama, unfortunately your suggestion of export const <var> = 0; is out of scope for Sucrase. Sucrase tries to match the behavior of TypeScript transpileModule as closely as possible (with minor exceptions), since it's meant to be a faster drop-in replacement. Babel, swc, and esbuild take the same approach, so this issue you're raising is a general issue (which already has some solutions, see below), not specific to Sucrase. See this playground link for how the output compares. If Sucrase did produce a runtime-visible export, it cause bugs for code iterating through a * import, or might conflict with an explicit value export.

The typical way to avoid the error you're seeing is to ensure that the code is removed on the import side. There are two historical ways this has been done:

  • Automatic import elision. The default behavior for both TypeScript and Sucrase is to detect that GlobOptions is only used as a type, and to remove that import line (or just the one named import) in the resulting JS. The isolatedModules TypeScript option can help flag some issues like re-exports that can cause issues here.
  • Explicit type syntax in the import. With Sucrase keepUnusedImports and TypeScript verbatimModuleSyntax (and its deprecated alternatives), the way to remove type imports is to explicitly write type GlobOptions.

Happy to reconsider if I'm overlooking a case where TypeScript (or another tool) emits a const declaration like you're talking about, but generally Sucrase follows TypeScript's lead on how file-by-file transpilation should work.

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

3 participants