Skip to content

Commit b9a18fd

Browse files
author
Andy
authored
Merge pull request microsoft#12672 from Microsoft/import_star_namespace
Properly determine whether an augmentation is a ValueModule or NamespaceModule
2 parents 8144c89 + a68a7ce commit b9a18fd

File tree

6 files changed

+46
-8
lines changed

6 files changed

+46
-8
lines changed

src/compiler/binder.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1512,7 +1512,7 @@ namespace ts {
15121512
errorOnFirstToken(node, Diagnostics.export_modifier_cannot_be_applied_to_ambient_modules_and_module_augmentations_since_they_are_always_visible);
15131513
}
15141514
if (isExternalModuleAugmentation(node)) {
1515-
declareSymbolAndAddToSymbolTable(node, SymbolFlags.NamespaceModule, SymbolFlags.NamespaceModuleExcludes);
1515+
declareModuleSymbol(node);
15161516
}
15171517
else {
15181518
let pattern: Pattern | undefined;
@@ -1534,12 +1534,8 @@ namespace ts {
15341534
}
15351535
}
15361536
else {
1537-
const state = getModuleInstanceState(node);
1538-
if (state === ModuleInstanceState.NonInstantiated) {
1539-
declareSymbolAndAddToSymbolTable(node, SymbolFlags.NamespaceModule, SymbolFlags.NamespaceModuleExcludes);
1540-
}
1541-
else {
1542-
declareSymbolAndAddToSymbolTable(node, SymbolFlags.ValueModule, SymbolFlags.ValueModuleExcludes);
1537+
const state = declareModuleSymbol(node);
1538+
if (state !== ModuleInstanceState.NonInstantiated) {
15431539
if (node.symbol.flags & (SymbolFlags.Function | SymbolFlags.Class | SymbolFlags.RegularEnum)) {
15441540
// if module was already merged with some function, class or non-const enum
15451541
// treat is a non-const-enum-only
@@ -1560,6 +1556,15 @@ namespace ts {
15601556
}
15611557
}
15621558

1559+
function declareModuleSymbol(node: ModuleDeclaration): ModuleInstanceState {
1560+
const state = getModuleInstanceState(node);
1561+
const instantiated = state !== ModuleInstanceState.NonInstantiated;
1562+
declareSymbolAndAddToSymbolTable(node,
1563+
instantiated ? SymbolFlags.ValueModule : SymbolFlags.NamespaceModule,
1564+
instantiated ? SymbolFlags.ValueModuleExcludes : SymbolFlags.NamespaceModuleExcludes);
1565+
return state;
1566+
}
1567+
15631568
function bindFunctionOrConstructorType(node: SignatureDeclaration): void {
15641569
// For a given function symbol "<...>(...) => T" we want to generate a symbol identical
15651570
// to the one we would get for: { <...>(...): T }

src/compiler/checker.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -486,6 +486,9 @@ namespace ts {
486486
}
487487
recordMergedSymbol(target, source);
488488
}
489+
else if (target.flags & SymbolFlags.NamespaceModule) {
490+
error(source.valueDeclaration.name, Diagnostics.Cannot_augment_module_0_with_value_exports_because_it_resolves_to_a_non_module_entity, symbolToString(target));
491+
}
489492
else {
490493
const message = target.flags & SymbolFlags.BlockScopedVariable || source.flags & SymbolFlags.BlockScopedVariable
491494
? Diagnostics.Cannot_redeclare_block_scoped_variable_0 : Diagnostics.Duplicate_identifier_0;

src/compiler/diagnosticMessages.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1823,6 +1823,10 @@
18231823
"category": "Error",
18241824
"code": 2609
18251825
},
1826+
"Cannot augment module '{0}' with value exports because it resolves to a non-module entity.": {
1827+
"category": "Error",
1828+
"code": 2649
1829+
},
18261830
"Cannot emit namespaced JSX elements in React": {
18271831
"category": "Error",
18281832
"code": 2650
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/node_modules/@types/lib-extender/index.d.ts(2,16): error TS2649: Cannot augment module 'lib' with value exports because it resolves to a non-module entity.
2+
3+
4+
==== /node_modules/lib/index.d.ts (0 errors) ====
5+
declare var lib: () => void;
6+
declare namespace lib {}
7+
export = lib;
8+
9+
==== /node_modules/@types/lib-extender/index.d.ts (1 errors) ====
10+
import * as lib from "lib";
11+
declare module "lib" {
12+
~~~~~
13+
!!! error TS2649: Cannot augment module 'lib' with value exports because it resolves to a non-module entity.
14+
export function fn(): void;
15+
}
16+

tests/baselines/reference/globalAugmentationModuleResolution.types

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
export { };
44

55
declare global {
6-
>global : any
6+
>global : typeof global
77

88
var x: number;
99
>x : number
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// @Filename: /node_modules/lib/index.d.ts
2+
declare var lib: () => void;
3+
declare namespace lib {}
4+
export = lib;
5+
6+
// @Filename: /node_modules/@types/lib-extender/index.d.ts
7+
import * as lib from "lib";
8+
declare module "lib" {
9+
export function fn(): void;
10+
}

0 commit comments

Comments
 (0)