question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

export as namespace doesn't support nesting namespaces

See original GitHub issue

I’m using rollup to generate a UMD module from my TypeScript source. It’s part of a bigger product, so one component exports to a root namespace (e.g. mylib) and another exports to a namespace nested under that (e.g. mylib.plugins.myplugin). Rollup is generating the necessary UMD logic to walk down from the global scope creating namespaces as needed, but I can’t model that in my TypeScript d.ts file.

The export as namespace syntax is working great in the first case, but not the second. It looks like TypeScript doesn’t support nested namespaces for this purpose. Is this by design or just an omission?

TypeScript Version: 2.7.0-dev.20180103

Code: Two files, a d.ts containing an export as namespace foo.bar declaration and a script that references it.

declaration.d.ts

export var baz;

export as namespace foo.bar;

app.ts

console.log(foo.bar.baz);

Compile with: tsc .\test.d.ts .\main.ts

Expected behavior: The file compiles correctly to the following JS:

console.log(foo.bar.baz);

Actual behavior: Error

declaration.d.ts(3,24): error TS1005: ';' expected.

If I change declaration.d.ts to use export as namespace foo (and update app.ts as needed), the compilation succeeds.

Issue Analytics

  • State:open
  • Created 6 years ago
  • Reactions:14
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

6reactions
mikezkscommented, Apr 19, 2019

My temp workaround is

// SDK.ts
export class SDK {}
export interface SDKOptions {}

// globals.d.ts
import * as sdk from "./SDK";
export as namespace Vendor;
export {sdk}

Results in Vendor.sdk.SDK, which I build by Webpack. Writing export as namespace Vendor.sdk in SDK.ts would be so much better.

What about this:

// SDK.ts
export class SDK {}
export interface SDKOptions {}
 
// index.ts
import * as _sdk from "./SDK";

export namespace Vendor {
    export import sdk = _sdk;
}

In this case you could avoid making it global and just use it after a normal ES6 import.

0reactions
kirill-konshincommented, Oct 26, 2018

My temp workaround is

// SDK.ts
export class SDK {}
export interface SDKOptions {}

// globals.d.ts
import * as sdk from "./SDK";
export as namespace Vendor;
export {sdk}

Results in Vendor.sdk.SDK, which I build by Webpack. Writing export as namespace Vendor.sdk in SDK.ts would be so much better.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Can't export nested namespaces - Stack Overflow
I'm trying to make the code cleaner by creating namespaces in different directories and then exporting them all through one namespace.
Read more >
Organizing TypeScript code using namespaces
TypeScript allows us to organize our code using nested namespaces. We can create nested namespaces as follows: namespace TransportMeans { export ...
Read more >
Namespaces - C# language specification - Microsoft Learn
A using_namespace_directive imports the types contained in the given namespace, but specifically does not import nested namespaces. Example: In ...
Read more >
Employing “Namespaces” in TypeScript to encapsulate your ...
These are called nested namespaces. // a.ts namespace MyLibA { export namespace Types { export interface Person { name: string; age: number; }...
Read more >
Documentation - Modules - TypeScript
Exporting a namespace from your module is an example of adding too many layers of nesting. While namespaces sometime have their uses, they...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found