-
Notifications
You must be signed in to change notification settings - Fork 12.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Re-exporting namespace declarations in ES6 ambient declaration #4336
Comments
so why use |
@mhegazy Sorry, I didn't understand your comment. Could you clarify? The |
We have talked about 1. allowing external modules to access the global namespace (#4166) and 2. extending what is the proposal here? |
I don't think #4166 applies here. I'm not trying to access the global namespace. I'm trying to re-export the exported declarations of an existing namespace. Extending As far as a proposal, I would suggest new syntax for ambient ES6 module declarations which allows re-exporting declarations from an existing namespace. Let me first illustrate with an example that already works: declare module "someLib" {
export var a: string;
}
declare module "myLib" {
export * from "someLib";
export var b: string;
} In that example,
My proposal would be the take that same concept and allow it for namespaces as well: declare namespace SomeLib {
export var a: string;
}
declare module "myLib" {
export * from SomeLib; // <-- this is the new syntax
export var b: string;
} Looking at the spec, I don't think there's anything exported from a namespace that can't also be exported from a module. In fact, namespace seems to be a subset of module:
Assuming that's the case, The use case for this is to simplify writing declaration files for existing ES6 libraries (see #4337). |
(Re)exporting namespaces or interfaces is really reasonable. current statusYou 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-exportingFor 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)... |
Any considerations on this? Something like the following would be extremely useful: //foo.ts
export enum Foo {
Foo,
Bar
}
export type TFoo = Foo | undefined;
export const FOO = 'FOO';
//global.ts
export namespace TEST {
export const SOME = 'SOME';
export namespace Nested {
export * from './foo.ts'; //error here
}
} |
(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... |
I'm currently using next workaround for interfaces: import {foo} from './foo'
export namespace NameSpace {
export interface Foo extends foo {}
} This should work for classes as well, the only drawback is the fact that you need to rename variables. |
I would also align typings with small modules while re exporting them in a accumulating bigger module. |
Oh! Ok! I came across this issue looking for a solution to this problem, but found later in my travels that there is actually a solution now. Like so: import FooTemp = Foo;
export namespace a {
export import Foo = FooTemp;
} |
Right now many declaration files take this form:
However, there doesn't seem to be an equivalent for ES6 modules:
The text was updated successfully, but these errors were encountered: