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.

Re-exporting namespace declarations in ES6 ambient declaration

See original GitHub issue

Right now many declaration files take this form:

declare namespace MyLib {

}

declare module 'myLib' {
  export = MyLib;
}

However, there doesn’t seem to be an equivalent for ES6 modules:

declare module 'myLib' {
  export default MyLib; // this works for exporting the default, but other exported items in MyLib are not considered to be named exports of the ES6 module
  export * from MyLib; // this is essentially what I'm trying to accomplish
}

Issue Analytics

  • State:open
  • Created 8 years ago
  • Reactions:20
  • Comments:10 (3 by maintainers)

github_iconTop GitHub Comments

34reactions
FlippieCoetsercommented, Feb 19, 2017

(re)exporting namespaces using existing export syntax would be helpful.

Exporting

export namespace NameSpace {
    export {foo} from './foo'
}

Allowing the above, could it help getting rid of the annoying Export declaration is not permitted in a namespace error?

Would love to logically organise a large component using namespace and still maintain the ability to expose needed members to the outside…

10reactions
e-cloudcommented, Dec 3, 2016

(Re)exporting namespaces or interfaces is really reasonable.


current status

You can use the follow style:

a.d.ts

export = class A {
}

index.d.ts

export import A = require('./a')

However, you can’t use it in a namespace which is incompatible with scene 3

And it usage is quite limited.


possible scene of re-exporting

For example, I may define several definiation files, assume the library name is MyLib:

a.d.ts

export function AFunc(): void

b.d.ts

export function BFunc(): void

scene 1 (re-export namespace as sub namespace)

index.d.ts (MyLib definition)

export * as LibA from './a'
export * as LibB from './b'

index.tests.ts

import * as MyLib from 'MyLib'
MyLib.LibA.AFunc()
MyLib.LibB.BFunc()

scene 2 (merge the namespaces)

index.d.ts (MyLib definition)

export * from './a'
export * from './b'

index.tests.ts

import * as MyLib from 'MyLib'
MyLib.AFunc()
MyLib.BFunc()

scene 3 (normal for node.js modules)

index.d.ts (MyLib definition)

declare namespace MyLib {
    export * as LibA from './a'
    export * as LibB from './b'
}

export = MyLib

index.tests.ts

import MyLib = require('MyLib')
MyLib.LibA.AFunc()
MyLib.LibB.BFunc()

scene 4 (like scene2 in scene3)

Read more comments on GitHub >

github_iconTop Results From Across the Web

Documentation - Namespaces and Modules - TypeScript
How to organize code in TypeScript via modules or namespaces. ... file could not be found, then the compiler will look for an...
Read more >
Re-export namespace from external module to global in ...
Here I'll post the piece of code to better explain my problem. //SomeClass.d.ts - I have to declare classes like this due to...
Read more >
Modules - TypeScript
Conversely, a file without any top-level import or export declarations is treated as a ... To do so, we use a construct similar...
Read more >
Jagruti Metaliya on Twitter: "- To do so, we use a construct similar to ...
Default export class and function declaration names are optional. ... We call declarations that don't define an implementation “ambient”.
Read more >
How To Use Namespaces in TypeScript | DigitalOcean
This means that multiple declarations of the same namespace will be merged into a single declaration. This can add flexibility to a namespace...
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