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

Omit type aliases when compiling TS #474

Closed
vforsh opened this issue Oct 20, 2020 · 3 comments
Closed

Omit type aliases when compiling TS #474

vforsh opened this issue Oct 20, 2020 · 3 comments

Comments

@vforsh
Copy link

vforsh commented Oct 20, 2020

I often use type aliases in my typescript files:
import Vector2Like = Types.Math.Vector2Like
after transpiling with esbuild it becomes:
var Vector2Like = Types.Math.Vector2Like

The issue is that Types.Math.Vector2Like exists only in the type space, there is no Types object in the runtime. And I get an error Uncaught TypeError: Cannot read property 'Math' of undefined.

Was it intentionally designed this way? If so - is there any way to fix it?

Thanks in advance!

@evmar
Copy link

evmar commented Oct 21, 2020

import in TS creates an alias in all three namespaces, which includes a value alias, which esbuild is reproducing here.

One workaround for your code is to instead write

type Vector2Like = Types.Math.Vector2Like;

to make it explicit that you want just a type alias.

@evanw
Copy link
Owner

evanw commented Oct 22, 2020

Thanks for reporting this issue, and thanks to @evmar for providing more details. I also wanted to ask: why not use a type statement here? Does that work for you?

Also, does this code compile without errors when isolatedModules is enabled? That flag is designed to prohibit TypeScript features that would interfere with tools that compile each TypeScript file independently such as esbuild and I think also Parcel and Babel. You should probably have isolatedModules enabled when using esbuild.

It would be interesting to see a full self-contained code sample that reproduces the problem. I don't have enough information from just this issue description to know what an appropriate fix could be, or even if it's possible to fix in a tool like esbuild that doesn't replicate TypeScript's type system.

@evanw
Copy link
Owner

evanw commented Nov 11, 2020

I looked more into this. It looks like it might be possible, although really hard, to get this to work for namespaces within the same file. Solving this basically involves building a mini type system of namespaces and import aliases and resolving references across them. From what I understand, getting this right might not require replicating TypeScript's full type system.

However, it seems like this feature is most often used for import aliases across files, which is not how esbuild works anyway. There is no way that will work with esbuild since esbuild converts each TypeScript file to JavaScript in isolation. Unfortunately it looks like TypeScript doesn't prevent these cross-file import aliases even when isolatedModules is enabled, which seems like a bug.

Right now I'm thinking about potentially removing support for the import x = y.z syntax from esbuild entirely (except the separate but similar import x = require() syntax, which would still work). You would have to use type x = y.z or const x = y.z as a replacement instead. There is potentially an option to only make it an error if esbuild can't find what the alias points to within the same file, although matching TypeScript's behavior gets extremely complicated with aliases-of-aliases and different nested scopes, so it may not be worth the complexity.

@evanw evanw closed this as completed in 7fe623e Nov 11, 2020
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