-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Empty namespace are removed causing code to break at runtime #1158
Comments
Thanks for the report. So namespaces in TypeScript are really weird. The "even if you have a reference to it" part isn't a reason to retain the namespace because namespaces that are empty and namespaces that only contain type declarations are not retained even if you have a reference to them. Also namespace ns {
export declare let L: any
console.log(L)
export declare function F(): any
console.log(F)
export declare class C {}
console.log(C)
export declare enum E {}
console.log(E)
export declare namespace N {}
console.log(N)
} The TypeScript compiler compiles that code to this: var ns;
(function (ns) {
console.log(ns.L);
console.log(F);
console.log(C);
console.log(E);
console.log(N);
})(ns || (ns = {})); As you can see, variables are special and behave differently than other declarations. So this is why esbuild's parser supports However, I wasn't aware of the case reported in this issue. I can potentially try to make this work. Alternatives in the meantime include using |
@evanw hm, I actually realized just now that When using Edit: ref https://www.typescriptlang.org/docs/handbook/declaration-files/by-example.html Btw, nice work. Esbuild is basically the only reason why I'm learning Go 😅 |
Description
I'm using TS namespaces in a project for the sole purpose of organizing code. The namespaces are technically empty at runtime (after build) and only contain type declarations. I've noticed my code failing because empty
namespace
's are removed even if you have a reference to it.Here is a minimal reproduction:
Example input:
TypeScript compiler output:
esbuild transform output:
This results in a ReferenceError thrown at runtime:
ReferenceError: Something is not defined
because the namespace is dropped because it's empty even though there is a reference to it (Something.Print = ...
andexport const { ... } = Something
)Expected output
Probably the same as TSC?
Version
ESBUILD VERSION: 0.11.11
The text was updated successfully, but these errors were encountered: