Skip to content

Commit 13cb66c

Browse files
authored
Merge pull request #13913 from Microsoft/allowExportDeclarationsInAmbientNamespaces
Allow export declarations in ambient namespaces
2 parents 477d9f2 + 4a6b6d0 commit 13cb66c

8 files changed

+108
-1
lines changed

src/compiler/checker.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -19540,7 +19540,9 @@ namespace ts {
1954019540
forEach(node.exportClause.elements, checkExportSpecifier);
1954119541

1954219542
const inAmbientExternalModule = node.parent.kind === SyntaxKind.ModuleBlock && isAmbientModule(node.parent.parent);
19543-
if (node.parent.kind !== SyntaxKind.SourceFile && !inAmbientExternalModule) {
19543+
const inAmbientNamespaceDeclaration = !inAmbientExternalModule && node.parent.kind === SyntaxKind.ModuleBlock &&
19544+
!node.moduleSpecifier && isInAmbientContext(node);
19545+
if (node.parent.kind !== SyntaxKind.SourceFile && !inAmbientExternalModule && !inAmbientNamespaceDeclaration) {
1954419546
error(node, Diagnostics.Export_declarations_are_not_permitted_in_a_namespace);
1954519547
}
1954619548
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//// [exportDeclarationsInAmbientNamespaces.ts]
2+
3+
declare namespace Q {
4+
function _try(method: Function, ...args: any[]): any;
5+
export { _try as try };
6+
}
7+
8+
Q.try(() => { });
9+
10+
11+
12+
//// [exportDeclarationsInAmbientNamespaces.js]
13+
Q["try"](function () { });
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
=== tests/cases/compiler/exportDeclarationsInAmbientNamespaces.ts ===
2+
3+
declare namespace Q {
4+
>Q : Symbol(Q, Decl(exportDeclarationsInAmbientNamespaces.ts, 0, 0))
5+
6+
function _try(method: Function, ...args: any[]): any;
7+
>_try : Symbol(_try, Decl(exportDeclarationsInAmbientNamespaces.ts, 1, 21))
8+
>method : Symbol(method, Decl(exportDeclarationsInAmbientNamespaces.ts, 2, 18))
9+
>Function : Symbol(Function, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --))
10+
>args : Symbol(args, Decl(exportDeclarationsInAmbientNamespaces.ts, 2, 35))
11+
12+
export { _try as try };
13+
>_try : Symbol(try, Decl(exportDeclarationsInAmbientNamespaces.ts, 3, 12))
14+
>try : Symbol(try, Decl(exportDeclarationsInAmbientNamespaces.ts, 3, 12))
15+
}
16+
17+
Q.try(() => { });
18+
>Q.try : Symbol(Q.try, Decl(exportDeclarationsInAmbientNamespaces.ts, 3, 12))
19+
>Q : Symbol(Q, Decl(exportDeclarationsInAmbientNamespaces.ts, 0, 0))
20+
>try : Symbol(Q.try, Decl(exportDeclarationsInAmbientNamespaces.ts, 3, 12))
21+
22+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
=== tests/cases/compiler/exportDeclarationsInAmbientNamespaces.ts ===
2+
3+
declare namespace Q {
4+
>Q : typeof Q
5+
6+
function _try(method: Function, ...args: any[]): any;
7+
>_try : (method: Function, ...args: any[]) => any
8+
>method : Function
9+
>Function : Function
10+
>args : any[]
11+
12+
export { _try as try };
13+
>_try : (method: Function, ...args: any[]) => any
14+
>try : (method: Function, ...args: any[]) => any
15+
}
16+
17+
Q.try(() => { });
18+
>Q.try(() => { }) : any
19+
>Q.try : (method: Function, ...args: any[]) => any
20+
>Q : typeof Q
21+
>try : (method: Function, ...args: any[]) => any
22+
>() => { } : () => void
23+
24+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
tests/cases/compiler/exportDeclarationsInAmbientNamespaces2.ts(7,23): error TS1194: Export declarations are not permitted in a namespace.
2+
3+
4+
==== tests/cases/compiler/exportDeclarationsInAmbientNamespaces2.ts (1 errors) ====
5+
6+
declare module "mod" {
7+
export var x: number;
8+
}
9+
10+
declare namespace N {
11+
export { x } from "mod"; // Error
12+
~~~~~
13+
!!! error TS1194: Export declarations are not permitted in a namespace.
14+
}
15+
16+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//// [exportDeclarationsInAmbientNamespaces2.ts]
2+
3+
declare module "mod" {
4+
export var x: number;
5+
}
6+
7+
declare namespace N {
8+
export { x } from "mod"; // Error
9+
}
10+
11+
12+
13+
//// [exportDeclarationsInAmbientNamespaces2.js]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
2+
declare namespace Q {
3+
function _try(method: Function, ...args: any[]): any;
4+
export { _try as try };
5+
}
6+
7+
Q.try(() => { });
8+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
2+
declare module "mod" {
3+
export var x: number;
4+
}
5+
6+
declare namespace N {
7+
export { x } from "mod"; // Error
8+
}
9+

0 commit comments

Comments
 (0)