Microsoft/TypeScript

'Export as namespace' issue in generating ambient code of same namespace

Open

#47.220 geöffnet am 22. Dez. 2021

Auf GitHub ansehen
 (0 Kommentare) (1 Reaktion) (0 zugewiesene Personen)TypeScript (6.726 Forks)batch import
BugDomain: JS EmitHelp Wanted

Repository-Metriken

Stars
 (48.455 Stars)
PR-Merge-Metriken
 (Durchschn. Merge 6T 17h) (9 gemergte PRs in 30 T)

Beschreibung

Bug Report

When using "export as namespace" feature in a declaration file, the exported classes are working as intended in 'legacy' ambient-style TS code that is still using namespaces, but the emitted .JS code lacks of the namespace in the case of namespace merging.

🔎 Search Terms

namespaces, modules, export as

🕗 Version & Regression Information

Typescript 4.5.4

  • It is replicable on 3.9.7 as well, so it doesn't seems a recent regression.

⏯ Playground Link

Playground link with relevant code

💻 Code

// api.d.ts
export declare class Class1 { }
export as namespace ns;

// test.ts
/// <reference path="./api.d.ts">
const x = new ns.Class1();
// Merging namespaces
namespace ns {
   const x = new Class1();
}

🙁 Actual behavior

The code above compiles without errors. However, the emitted JS is not using the ns object, when augmenting the ns namespace:

const x = new ns.Class1();  // <-- this is correct
var ns;
(function (ns) {
    const x = new Class1();   // <-- Class1 is undefined, ns.Class1 should be used instead
})(ns || (ns = {}));

🙂 Expected behavior

const x = new ns.Class1();
var ns;
(function (ns) {
    const x = new ns.Class1();
})(ns || (ns = {}));

Thanks!

Contributor Guide