Repository navigation
Remove exported TS types named like JavaScript globals - #18501
Conversation
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.
|
Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/62931 |
|
commit: |
JLHwung
left a comment
There was a problem hiding this comment.
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 {}:
-
GLOBAL_TYPESalso contains ambient values, because the pre-pass registersdeclare 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?
-
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.
|
@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 } |
|
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 |
JLHwung
left a comment
There was a problem hiding this comment.
OK, given that main is already failing on those two, I am fine to merge this one as-is.
|
Backported to 7.x in #18601. |
Fixes #18354
isGlobalTypestarts withscope.hasBinding(name), which also returns true for builtin names likePromise,MapandError. So an exportedinterface Promise {}ortype 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 Errorsilently exported the globalErrorconstructor.The check now ignores builtins first (
noGlobals: true), then looks for a declared type, and only then falls back to builtins, soexport default Mathandexport default undefinedstill 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.