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

How to properly import enums #2298

Closed
rexkenley opened this issue Jun 3, 2022 · 2 comments
Closed

How to properly import enums #2298

rexkenley opened this issue Jun 3, 2022 · 2 comments

Comments

@rexkenley
Copy link

Hi

I am having an issue with importing enums using esbuild.

I have an xrm.d.ts file that has an enum that I want to use
image

But I am not sure how to import it to trigger the esbuild inline function.
image

This is the resulting build that is failing
image

Thanks for the help and making this awesome tool.

@evanw
Copy link
Owner

evanw commented Jun 3, 2022

I believe this is happening because with esbuild, type annotations have no effect on your compilation as esbuild does not reimplement the TypeScript type checker. Everything in a .d.ts file is just a type annotation that serves to describe some values available in JavaScript code to the type checker. If the type annotations describe things that don't actually exist in JavaScript, then your code will potentially pass the type checker but crash at run-time when the resulting JavaScript code is run. This limitation is not specific to esbuild. Other TypeScript-aware build tools such as Babel also don't reimplement the TypeScript type checker, and also won't be able to handle this.

Some context: TypeScript's list of non-goals includes "[do not] emit different code based on the results of the type system" so type annotations are not supposed to affect the emitted JavaScript code. Babel and esbuild follow this principle. Code like yours (specifically with declare const enum) happens to work with the official TypeScript compiler because the TypeScript team deviated from their own non-goals when they implemented declare const enum, which does actually affect the emitted JavaScript code even though it's just a type annotation. This is regarded as a design mistake by the TypeScript team but they can't remove it because they don't want to break backward compatibility.

TypeScript does have a feature that can make doing this a type checking error so that your code won't pass the type checker only to crash at run-time. You have to enable "isolatedModules": true in your tsconfig.json file. This is documented here:

But that will only make this a type checking error and prevent you from building your code, which won't actually fix your problem. It's not clear to me what you need to do to fix this because you didn't link to the code for the library you're using or to the error message you're getting.

I'm guessing that the library you're using makes Xrm a global variable, but Xrm.FormType currently only exists in the type system and not in JavaScript. In that case the library would need to be updated to fix this properly. The authors could add a Xrm.FormType object with the correct properties to get this to work. In the meantime, you could copy the const enum FormType declaration into your code (outside of a .d.ts file and outside of a declare block, so it's not a type annotation) and use that to get it to work. Or, if you don't want to change your code, you could not use esbuild and use tsc instead (or use tsc to convert .ts to .js and then use esbuild to bundle the .js).

@rexkenley
Copy link
Author

That's the most thorough answer I have ever seen. Thanks!

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

2 participants