You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
export declare... what that even is supposed to mean? π The non-ESM module transforms emit this based on the logic in substituteExpressionIdentifier (that calls into resolver.getReferencedExportContainer, which returns exportContainer.kind === SyntaxKind.SourceFile). This is something that's recorded as an export of that source file, so it seems to be "available" as an export/import.
When this is imported by other modules the emit for them includes appropriate importing statements:
export declare is indeed extremely confusing. I think the best interpretation is effectively "Pretend that someone text-concats an equivalent real export declaration somewhere in this file", which is load-bearing on "equivalent"
Looking at the emit again, I'm not super sure what the defect is actually supposed to be.
For CJS we have
exports.a=2;
which is a correct output; it's the same as if you wrote
exportleta: number=3;a=2;
which produces
exports.a=3;exports.a=2;
so we're exactly corresponding to the second line
For ESM we have
a=2;
which is correct according to the "pretend that someone text-concated" interpretation of export declare
Absent a stronger argument, this is maybe just working as intended?
π 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
a
exported), sincedeclare
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.
The text was updated successfully, but these errors were encountered: