Skip to content

Remove exported TS types named like JavaScript globals - #18501

Merged
nicolo-ribaudo merged 1 commit into
babel:mainfrom
TemRevil:fix/ts-export-type-named-like-global
Nov 11, 2026
Merged

nicolo-ribaudo merged 1 commit into
babel:mainfrom
TemRevil:fix/ts-export-type-named-like-global

Conversation

@TemRevil

@TemRevil TemRevil commented Nov 2, 2026

Copy link
Copy Markdown
Contributor

Fixes #18354

isGlobalType starts with scope.hasBinding(name), which also returns true for builtin names like Promise, Map and Error. So an exported interface Promise {} or type Map = ... was treated as a value and its export was kept. The output then exports a name the module no longer declares, which Node rejects as an early error. export default Error silently exported the global Error constructor.

The check now ignores builtins first (noGlobals: true), then looks for a declared type, and only then falls back to builtins, so export default Math and export default undefined still work.

Added a fixture, exports/type-named-like-global. It fails without the change (the exports are kept) and passes with it. The rest of the transform-typescript suite passes.

Scope#hasBinding also returns true for builtin names, so an exported interface or type named Promise, Map or Error was treated as a value and its export specifier was kept.
@babel-bot

Copy link
Copy Markdown
Collaborator

Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/62931

@pkg-pr-new

pkg-pr-new Bot commented Nov 2, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

commit: 9482cd3

@JLHwung JLHwung left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for working on this! The interface and type cases are indeed fixed, however, moving the GLOBAL_TYPES check ahead of the builtin fallback causes two new regressions. An export that used to match tsc on the main branch is now silently replaced with export {}:

  1. GLOBAL_TYPES also contains ambient values, because the pre-pass registers declare var/const/class/function/enum/namespace. These describe real runtime globals, so their exports need to stay:

    declare const Promise: any;
    export default Promise;
    declare class Map<K, V> {}
    export { Map };

Could we track type-only names (interface, type, import type) separately from ambient values, and only check the type-only names before the builtin fallback?

  1. A value namespace has no scope binding until it is visited. If it merges with a builtin-named interface and comes after the export, the export of the runtime value is dropped:

    interface Error { code: number }
    export default Error;
    namespace Error { export const a = 1 }

The pre-pass needs to recognize value namespaces up front, so the result doesn't depend on visit order.

For both regressions, please also add these cases to the test fixtures, with the expected output checked against tsc, if the current test fixtures doesn't cover them.

@nicolo-ribaudo

Copy link
Copy Markdown
Member

@JLHwung those cases are already broken in Babel with any other export name:

declare const Foo: any;
export default Foo;
interface Foo { code: number }
export default Foo;
namespace Foo { export const a = 1 }

@nicolo-ribaudo nicolo-ribaudo added 7.x: needs backport PR: Bug Fix 🐛 A type of pull request used for our changelog categories labels Nov 11, 2026
@nicolo-ribaudo

Copy link
Copy Markdown
Member

Also note that with TSC, this example:

declare class Map<K, V> {}
export { Map };

generates this code:

export { Map }

which is not valid and will throw at runtime, as Map is not lexically defined in that file.

@JLHwung JLHwung left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, given that main is already failing on those two, I am fine to merge this one as-is.

@nicolo-ribaudo
nicolo-ribaudo added this pull request to the merge queue Nov 11, 2026
Merged via the queue into babel:main with commit db68099 Nov 11, 2026
134 of 160 checks passed
@babel-bot

Copy link
Copy Markdown
Collaborator

Backported to 7.x in #18601.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

PR: Bug Fix 🐛 A type of pull request used for our changelog categories

Projects

None yet

Development

Successfully merging this pull request may close these issues.

TypeScript transform keeps an export specifier of a type named like a JavaScript builtin

4 participants