Skip to content

export declare sometimes affects JS emitΒ #60932

Closed
@nicolo-ribaudo

Description

@nicolo-ribaudo

πŸ”Ž 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugA bug in TypeScriptHelp WantedYou can do this

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions