🔎 Search Terms
export declare js emit
🕗 Version & Regression Information
- This is the behavior in every version I tried
⏯ Playground Link
https://www.typescriptlang.org/play/?module=1#code/KYDwDg9gTgLgBAE2AYwDYEMrDq490BccAdgK4C2ARsFANwBQ6cAvHAEwNA
💻 Code
export declare let a: number;
a = 2;
🙁 Actual behavior
When compiled to JS, will that code export a variable named a? It depends!
- if compiling to ESM, no
- if compiling to CJS/AMD, yes
- if inside a
namespace, yes
🙂 Expected behavior
Compiling to ESM or to CJS/AMD should behave the same. Specifically, they should all have the ESM behavior (no a exported), since declare means "this statement is only to tell something to the type checker, it should not actually affect the emitted JS code".
Maybe referencing locally something declared as export declare should be an error.
Additional information about the issue
Babel (babel/babel#17038), SWC (swc-project/swc#9821), and OXC (https://playground.oxc.rs/#eNo9TjFuwzAM/IrA2UNToIuKrp37gC60QgcGJFIg5TSB4b+XiuNo0R15d7wVEkRgLGQVEwW2sP5y8Ee3KtrCmVJGpZCpBYyBlzKSfu4SDF/h5HiDAQTiCrpw/+zODW8Qmy40QJ65QZwwmxNLUunY2L2Mkg/WFNkm0bIPtgEqqpH2xKrk6Eo/XoXtmeaKnv1QOH75d0tDvZAfBrL3t9MHuCLJmS7UKzopM8/TfJiTcFPJ31n+uvlKOop50f3Qtv0D8iJmOg==) already have the behavior I'm proposing.
🔎 Search Terms
export declare js emit
🕗 Version & Regression Information
⏯ Playground Link
https://www.typescriptlang.org/play/?module=1#code/KYDwDg9gTgLgBAE2AYwDYEMrDq490BccAdgK4C2ARsFANwBQ6cAvHAEwNA
💻 Code
🙁 Actual behavior
When compiled to JS, will that code export a variable named
a? It depends!namespace, yes🙂 Expected behavior
Compiling to ESM or to CJS/AMD should behave the same. Specifically, they should all have the ESM behavior (no
aexported), sincedeclaremeans "this statement is only to tell something to the type checker, it should not actually affect the emitted JS code".Maybe referencing locally something declared as
export declareshould be an error.Additional information about the issue
Babel (babel/babel#17038), SWC (swc-project/swc#9821), and OXC (https://playground.oxc.rs/#eNo9TjFuwzAM/IrA2UNToIuKrp37gC60QgcGJFIg5TSB4b+XiuNo0R15d7wVEkRgLGQVEwW2sP5y8Ee3KtrCmVJGpZCpBYyBlzKSfu4SDF/h5HiDAQTiCrpw/+zODW8Qmy40QJ65QZwwmxNLUunY2L2Mkg/WFNkm0bIPtgEqqpH2xKrk6Eo/XoXtmeaKnv1QOH75d0tDvZAfBrL3t9MHuCLJmS7UKzopM8/TfJiTcFPJ31n+uvlKOop50f3Qtv0D8iJmOg==) already have the behavior I'm proposing.