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

add more special cases to ts namespace transform #1161

Merged
merged 1 commit into from
Apr 17, 2021
Merged

add more special cases to ts namespace transform #1161

merged 1 commit into from
Apr 17, 2021

Conversation

evanw
Copy link
Owner

@evanw evanw commented Apr 17, 2021

With this PR, esbuild's TypeScript-to-JavaScript transform will no longer omit the namespace in this case:

namespace Something {
  export declare function Print(a: string): void
}
Something.Print = function(a) {}

This was previously omitted because TypeScript omits empty namespaces, and the namespace was considered empty because the export declare function statement isn't "real":

namespace Something {
  export declare function Print(a: string): void
  setTimeout(() => Print('test'))
}
Something.Print = function(a) {}

The TypeScript compiler compiles the above code into the following:

var Something;
(function (Something) {
  setTimeout(() => Print('test'));
})(Something || (Something = {}));
Something.Print = function (a) { };

Notice how Something.Print is never called, and what appears to be a reference to the Print symbol on the namespace Something is actually a reference to the global variable Print. I can only assume this is a bug in TypeScript, but it's important to replicate this behavior inside esbuild for TypeScript compatibility.

The TypeScript-to-JavaScript transform in esbuild has been updated to match the TypeScript compiler's output in both of these cases.

Fixes #1158

@evanw evanw merged commit d0ab9aa into master Apr 17, 2021
@evanw evanw deleted the issues/1158 branch April 17, 2021 09:49
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

Successfully merging this pull request may close these issues.

Empty namespace are removed causing code to break at runtime
1 participant