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

[Feature Request] Try resolve const enum from .d.ts #3359

Closed
yangmingshan opened this issue Aug 30, 2023 · 4 comments
Closed

[Feature Request] Try resolve const enum from .d.ts #3359

yangmingshan opened this issue Aug 30, 2023 · 4 comments

Comments

@yangmingshan
Copy link

Problem

I have a project base on @vue/reactivity, and @vue/reactivity contains const enum. I want to use esbuild to build my project, but I can't, because esbuild can't resolve const enum from @vue/reactivity. I'm guessing this is because the const enum only exist in the type file and not source file.

Request

So, is it possible to resolve package's const enum from the package's type file?

@yangmingshan
Copy link
Author

Related issue: vitest-dev/vitest#1650

@magic-akari
Copy link
Contributor

maybe duplicate: #2298 (comment)

@evanw
Copy link
Owner

evanw commented Sep 3, 2023

I'm guessing this is because the const enum only exist in the type file and not source file.

Yes, this is the problem. Here's Vite's documentation about this from the link you pasted:

TypeScript Compiler Options

Some configuration fields under compilerOptions in tsconfig.json require special attention.

isolatedModules

Should be set to true.

It is because esbuild only performs transpilation without type information, it doesn't support certain features like const enum and implicit type-only imports.

You must set "isolatedModules": true in your tsconfig.json under compilerOptions, so that TS will warn you against the features that do not work with isolated transpilation.

If you set isolatedModules to true and attempt to use a declare const enum from @vue/reactivity, TypeScript should give you a compile error. This is telling you that the library you are using is incompatible with tools such as esbuild that compile each module in isolation. This issue needs to be fixed in the library you are trying to use (I see you have already found vuejs/core#1228 about fixing this for Vue).

I'm closing this issue because I don't plan on implementing this in esbuild. While it's sort of possible to make this work in some simple cases, emulating this correctly breaks down in more complex cases without esbuild replicating TypeScript's type system, which I'm not going to do. I have to draw a "line in the sand" somewhere and I have decided that for esbuild, the line is that esbuild does not attempt to interpret type annotations (and everything in a .d.ts file is a type annotation).

@evanw evanw closed this as not planned Won't fix, can't repro, duplicate, stale Sep 3, 2023
@yangmingshan
Copy link
Author

Thank you for your response, it makes perfect sense.

richardquaite added a commit to richardquaite/UITechTest that referenced this issue Dec 3, 2023
I'd like to use enums here.  I think an enum like
```
enum QuerystringKeys {
  SelectedMovieId = 'selected',
  ...
}
```
provides much better editor hinting for the developer.  Adding them to `includes` in tsconfig means they don't need to be imported to be used as a variable
```
useQuerystring(QuerystringKeys.SelectedMovieId);
```
but I ran into this issue evanw/esbuild#3359
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